How do you test executable Haskell files

Haskell in 5 steps

Haskell is a universal and purely functional programming language. This page should help you get started as soon as possible.

Install Haskell

Haskell, like many other languages, comes in two different types: batch processed (Compiler) and interactive (interpreter). An interactive system provides you with a command line in which you can experiment and evaluate expressions directly. It is probably a good choice to start with.

GHCCompiler and Interpreter (GHCi) Probably the most complete system.
HugsInterpreter only Very portable and lighter than the GHC.

While both GHC and Hugs run on Windows, Hugs is probably best integrated with Windows. Nonetheless, GHC is being more actively developed and maintained. In addition, there seems to be a consensus that the GHC is the development environment of choice for beginners and old hands. There is also information on installing Haskell software on Mac OS X.

Start Haskell

Open a terminal. Tap ghci (the name of the GHC interpreter executable file) if you have GHC installed. If you have Hugs installed, give hugs a.

$ ghci ___ ___ _ / _ \ / \ / \ / __ (_) / / _ \ // / _ / / / | | GHC Interactive, version 6.4, for Haskell98. / / _ \ / __ / / ___ | | http://www.haskell.org/ghc/ \ ____ / \ / / _ / \ ____ / | _ | Type:? for help. Loading package base-1.0 ... linking ... done. Prelude>

You will be shown a prompt and Haskell is now waiting for your input.

Write your first Haskell program

If you have learned another programming language, your first program was probably "Hello, World!". Let's start with that.

Prelude> "Hello, World!" "Hello, World!"

Haskell evaluates the string and displays the result. We can also vary and display it directly from the standard output:

Prelude> putStrLn "Hello World" HelloWorld

With a Haskell compiler like the GHC, you can compile the source code into an executable single application. Create a file hello.hs with the content:

main = putStrLn "Hello, World!"

And compile it with:

$ ghc -o hello hello.hs

You can now execute the result (./Hello under Unix systems, hello.exe under Windows):

$ ./hello Hello, World!

Haskell as a calculator

Now for something more interesting. Your first real Haskell program is the faculty function.

Prelude> letfacn = ifn == 0then1elsen * fac (n-1)

This defines a new function fac which computes the factorial of an integer.

We can now fac call with one argument:

Prelude> fac421405006117752879898543142606244511569936384000000000

Congratulations! Programming made easy. Note that in Hugs, the definition of fac from a file fac.hs that includes:

facn = ifn == 0then1elsen * fac (n-1)

In Hugs it needs to be done like this (this also works in GHCi):

Hugs.Base>: loadfac.hsMain> fac421405006117752879898543142606244511569936384000000000

Of course, we can also compile this program to create a stand-alone program. We can now go into the file using pattern matching fac.hs write the following:

fac0 = 1facn = n * fac (n-1) main = print (fac42)

This can then be compiled and executed:

$ ghc -o fac fac.hs $ ./fac 1405006117752879898543142606244511569936384000000000

Great!

Write your first parallel Haskell program

Haskell has good support for parallel and multicore programming. We write a parallel Haskell program by adding `par` to the respective expression.

importControl.Parallelmain = a`par`b`par`c`pseq`print (a + b + c) wherea = ack310b = fac42c = fib34fac0 = 1facn = n * fac (n-1) ack0n = n + 1ackm0 = ack ( m-1) 1ackmn = ack (m-1) (ackm (n-1)) fib0 = 0fib1 = 1fibn = fib (n-1) + fib (n-2)

This program is then compiled with the -threaded flag and optimizations:

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

Now we can run our program, in this example with two cores:

$ time ./A + RTS -N2 1405006117752879898543142606244511569936384005711076 ./A + RTS -N2 2.14s user 0.02s system 149% cpu 1.449 total

Congratulations! Now you program your multicore processor!

How it goes on

There are plenty of good Haskell tutorials and books out there. Here are some we recommend:

Tutorials

A complete list of textbooks and specialist books as well as tutorials and other recommendations can be found here:

Join the community!

Chat with members of the Haskell community:

Languages: deenturzh / cnzh / twja