Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Haskell in 5 steps
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== 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: <haskell> Prelude> let fac n = if n == 0 then 1 else n * fac (n-1) </haskell> This defines a new function called '''fac''' which computes the factorial of an integer. We can now run '''fac''' on some argument: <haskell> Prelude> fac 42 1405006117752879898543142606244511569936384000000000 </haskell> '''Congratulations!''' Programming made easy. Note that if you're using Hugs, you'll need to load the definition of '''fac''' from a file, '''fac.hs''', containing: <haskell> fac n = if n == 0 then 1 else n * fac (n-1) </haskell> And run it with Hugs as follows (this also works in GHCi): <haskell> Hugs.Base> :load fac.hs Main> fac 42 1405006117752879898543142606244511569936384000000000 </haskell> We can of course compile this program, to produce a standalone executable. In the file '''fac.hs''' we can write (and let's use elegant pattern matching syntax just for fun): <haskell> fac 0 = 1 fac n = n * fac (n-1) main = print (fac 42) </haskell> which can then be compiled and run: <pre> $ ghc -o fac fac.hs $ ./fac 1405006117752879898543142606244511569936384000000000 </pre> '''Great!''' ===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: <haskell> 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) </haskell> Compiling with -threaded and optimizations on: <pre> $ ghc -O2 --make A.hs -threaded -rtsopts [1 of 1] Compiling Main ( A.hs, A.o ) Linking A ... </pre> ''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: <pre> $ time ./A +RTS -N2 1405006117752879898543142606244511569936384005711076 ./A +RTS -N2 2.14s user 0.02s system 149% cpu 1.449 total </pre> Congratulations! You are now programming your multicore! ==== Troubleshooting ==== If you encounter a <code>Could not find module ‘Control.Parallel’</code> error, please install the parallel library with <code>cabal install --lib parallel</code>
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width