Today’s challenge: write my first program using Haskell. Let’s start!
Searching “Hello World Haskell” on Google gives me the following tutorial: Haskell in 5 steps.
First step is to install the Haskell Platform. Main components are the GHC (Glasgow Haskell Compiler) and Cabal (Common Architecture for Building Applications and Libraries). I decided to use
brew for simplicity.
brew update brew install ghc brew install cabal-install
Using the REPL
You can run Haskell REPL using
$ ghci GHCi, version 7.10.1: http://www.haskell.org/ghc/ :? for help Prelude>
Here you can run any command:
Prelude> "Hello, World!" "Hello, World!" Prelude> putStrLn "Hello World" Hello World Prelude> 3 ^ 5 243 Prelude> :quit Leaving GHCi.
putStrLn "Hello World"
$ ghc hello.hs [1 of 1] Compiling Main ( hello.hs, hello.o ) Linking hello ...
Output executable is named
hello. You can run it as any other executable.
$ ./hello Hello, World!
Now a bit more code: factorial calculator. First step is to define factorial function. You can do in a single line:
let fac n = if n == 0 then 1 else n * fac (n-1)
Or split definition on multiple lines:
fac 0 = 1 fac n = n * fac (n-1)
And put everything into
factorial.hs. Now you can load the function inside the console:
Prelude> :load factorial.hs [1 of 1] Compiling Main ( factorial.hs, interpreted ) Ok, modules loaded: Main. *Main> fac 42 1405006117752879898543142606244511569936384000000000
Or write a
main function then compile and run your executable:
fac 0 = 1 fac n = n * fac (n-1) main :: IO () main = print (fac 42)
N.B. First time I compiled the source code I needed to add
main :: IO () to avoid compiling error:
The IO action 'main' is not defined in module 'Main'.
Now your executable runs well:
Haskell is cool because the pure functional nature and the parallel/multicore vocation so this beginner’s tutorial add some tips about that.
First of all you need to get the Parallel lib:
$ cabal update Downloading the latest package list from hackage.haskell.org $ cabal install parallel Resolving dependencies... Downloading parallel-184.108.40.206... Configuring parallel-220.127.116.11... Building parallel-18.104.22.168... Installed parallel-22.214.171.124
Then you can write your parallel software using
import Control.Parallel main = a `par` b `par` c `pseq` print (a + b + c) where a = ack 3 10 b = fac 42 c = fib 34 fac 0 = 1 fac n = n * fac (n-1) ack 0 n = n+1 ack m 0 = ack (m-1) 1 ack m n = ack (m-1) (ack m (n-1)) fib 0 = 0 fib 1 = 1 fib n = fib (n-1) + fib (n-2)
Compile it with
$ ghc -O2 --make parallel.hs -threaded -rtsopts [1 of 1] Compiling Main ( parallel.hs, parallel.o ) Linking parallel ...
And run it
./parallel +RTS -N2 1405006117752879898543142606244511569936384005711076
Now, many details aren’t clear to me. From the meaning of
`pseq` to the level of parallelism used. Moreover I have no idea of what are the options used with compiler and the ones for the executable. There is definitely more then “Hello, World!”.
Next step would be Haskell in 10 minutes, another beautiful introduction to the language with links for more complex topics, or Real World Haskell (by Bryan O’Sullivan, Don Stewart, and John Goerzen) or any other useful tutorial listed on 5 steps guide.
See you to the next language 🙂