Parallel Processing with Haskell

cpu usage

Download Haskell now. It's a pure functional language with built-in support for parallel processing. That means it is more Lispy than Lisp and faster than C.

import Text.Printf (printf)
import Control.Parallel (par, pseq)

main :: IO ()
main = a `par` b `par` c `pseq` printf "A = %d\nB = %d\nC = %d\n" a b c
    a = ack 3 10
    b = fac 42
    c = fib 34

fac :: Integer -> Integer
fac 0 = 1
fac n = n * fac (n-1)

ack :: Integer -> Integer -> Integer
ack 0 n = n + 1
ack m 0 = ack (m-1) 1
ack m n = ack (m-1) (ack m (n-1))

fib :: Integer -> Integer
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)


$ ghc -O2 --make maths.hs -threaded -rtsopts
[1 of 1] Compiling Main             ( maths.hs, maths.o )
Linking maths ...


$ time ./maths +RTS -N
A = 8189
B = 1405006117752879898543142606244511569936384000000000
C = 5702887

real	0m0.774s
user	0m1.201s
sys	0m0.014s


This program computes the ackermann, factorial, and fibonacci functions in parallel. par instructs Haskell to calculate a, b, and c independently. pseq instructs Haskell to calculate the values before printing them.

Compilation options
Runtime options

So there you have it. A parallel processing program with no threading headaches. Haskell does the grunt work for you so you can be more a productive programmer.

Resources is the official Haskell website.

Try Haskell is an online, interactive tutorial for learning Haskell.

Learn You a Haskell is a funny tutorial explaining list comprehensions.

Haskell in 5 steps inspired this tutorial.