Tuesday, May 24, 2011

[qfzrotzy] Factoring via readProcess

A quick example of using readProcess in Haskell to "shell out" an integer factorization task, calling the Linux "factor" command (extracted from the Prime Day calculator):

import System.Process;

factor :: Integer -> IO([](Integer));
factor x = ((readProcess "factor" [(show x)] "") >>= (return . tail . (map read) . words));

A better way might use the Pari library.

2 comments :

Anonymous said...

What happened to the type signature?

factor :: Integer -> IO([](Integer));

should obviously be

factor :: Integer -> IO [Integer]

Ken said...

It may be funny-looking, but it does parse!

I use a (unpublished) preprocessor that emits types in "prefix" form, even for the list type constructor [].