Haskell in 5 steps
Haskell is a general purpose, purely functional programming language. This page will help you get started as quickly as possible.Contents[hide] |
1 Install Haskell
The recommended way to get started with programming Haskell is the Haskell Platform. The Platform comes with GHC, the de-facto standard Haskell compiler, with many useful tools that will let you program Haskell painlessly. The installation should be easy and simple enough on most operating systems.Try Haskell provides a less complete but quicker way to give Haskell a shot.
2 Start Haskell
If you have installed the Haskell Platform, open a terminal and type ghci (the name of the executable of the GHC interpreter) at the command prompt. Alternatively, if you are on Windows, you may choose WinGHCi in the Start menu.$ ghci GHCi, version 6.12.3: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. Prelude>And you are presented with a prompt. The Haskell system now attentively awaits your input.
3 Write your first Haskell program
If you've learned to program another language, your first program probably was "Hello, world!", so let's do that:Prelude> "Hello, World!" "Hello, World!"
Prelude> putStrLn "Hello World" Hello World
main = putStrLn "Hello, World!"
$ ghc -o hello hello.hsYou can then run the executable (./hello on Unix systems, hello.exe on Windows):
$ ./hello Hello, World!
4 Haskell the calculator
Let's do something fun. In Haskell, your first true program is the factorial function. So back to the interpreter now and let's define it:Prelude> let fac n = if n == 0 then 1 else n * fac (n-1)
We can now run fac on some argument:
Prelude> fac 42 1405006117752879898543142606244511569936384000000000
fac n = if n == 0 then 1 else n * fac (n-1)
Hugs.Base> :load fac.hs Main> fac 42 1405006117752879898543142606244511569936384000000000
fac 0 = 1 fac n = n * fac (n-1) main = print (fac 42)
$ ghc -o fac fac.hs $ ./fac 1405006117752879898543142606244511569936384000000000Great!
4.1 Write your first parallel Haskell program
Haskell has good support for parallel and multicore programming. We can write a parallel program by adding `par` to expressions, like so: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)
$ ghc -O2 --make A.hs -threaded -rtsopts [1 of 1] Compiling Main ( A.hs, A.o ) Linking A ...Note that in versions of GHC prior to GHC 7, you can leave off the -rtsopts flag
And now we can run our multicore program. Here across two cores:
$ time ./A +RTS -N2 1405006117752879898543142606244511569936384005711076 ./A +RTS -N2 2.14s user 0.02s system 149% cpu 1.449 totalCongratulations! You are now programming your multicore!
5 Where to go from here
There are many good Haskell tutorials and books. Here are some we recommend:Tutorials
- Real World Haskell
- Haskell in 10 minutes
- Yet Another Haskell Tutorial (English)
- A Gentle Introduction to Haskell (English, Image:GentleFR.pdf)
- Learn You A Haskell For Great Good!
Join the community!
Talk to others in the Haskell community.
Languages: de tur zh/cn zh/tw ja
No comments:
Post a Comment
Thank you