The functional programming language Haskell provides a very easy way of parallelization.
Consider the following naive implementation of the Fibonacci function.
fib0=0fib1=1fibn=fib(n-1)+fib(n-2)
This implementation has a bad expontential time complexity, so it should be improved, for example with caching. But this is beyond the scope of this article. We just need a function that takes a while to finish.
In Haskell there are two operators that have to be used for parallelization: par and pseq. par a b is some kind of a “fork” operation: a is started in parallel and b is returned. Keep in mind that Haskell is has a lazy evaluation strategy. a is only evaluated if it is needed The function pseq a b evaluates first a then b.
Equipped with this two operations it is very easy to parallelize fib.
parfibn|n<11=fibn-- For small values of n we use the sequential version
|otherwise=f1`par`(f2`pseq`(f1+f2))-- calculate in parallel, return the sum
wheref1=parfib(n-1)f2=parfib(n-2)
The code has to be compiled with the -threaded option.
$ ghc -O3-threaded--make-o parfib ParFib.hs
The number of threads is specified at runtime with the -N command line option.
$ ./parfib +RTS -N7-RTS
On an Intel Core i7 920 this resulted in a speedup of 4.13 for n=38. This processor has four physical cores. So this is efficient. Haskell is still one of the best programming languages.
I’ve written an article for JavaSpektrum 2/2025 about Structured Concurrency in Java 24. The article covers JEP 499, Java’s fourth preview of Structured Concurrency, which aims to make concurrent programming...
Concurrent and asynchronous applications can be written in Kotlin with coroutines in a precise and structured way. Unfortunately, concurrency often increases the complexity and it is difficult to create bug-free...