Difference between revisions of "Haskell in 5 steps"

From HaskellWiki
Jump to navigation Jump to search
(wibble)
(Simpler, cleaner 5 step program)
Line 1: Line 1:
Haskell is a general purpose, purely functional programming language. This page will help you get started as quickly as possible.
+
Haskell is a general purpose, purely functional programming language.
  +
This page will help you get started as quickly as possible.
   
 
__TOC__
 
__TOC__
 
== Why learn Haskell? ==
 
 
John Hughes has written a good essay: [http://www.md.chalmers.se/~rjmh/Papers/whyfp.html Why Functional Programming Matters].
 
   
 
== Install Haskell ==
 
== Install Haskell ==
   
Haskell, like most other languages, comes in two flavors: batch oriented (''compiler'') and interactive (''interpreter''). An interactive system gives you a command line where you can experiment and evaluate expressions directly, and is probably a good choice.
+
Haskell, like most other languages, comes in two flavors: batch oriented
  +
(''compiler'') and interactive (''interpreter''). An interactive system
  +
gives you a command line where you can experiment and evaluate
  +
expressions directly, and is probably a good choice to start with.
   
 
{| class="wikitable"
 
{| class="wikitable"
Line 21: Line 21:
 
|}
 
|}
   
While both GHC and Hugs work on Windows, Hugs has perhaps the best integration on that platform. There is also information available on [[Mac OS X|installing Haskell software on Mac OS X]].
+
While both GHC and Hugs work on Windows, Hugs has perhaps the best
  +
integration on that platform. There is also information available on
  +
[[Mac OS X|installing Haskell software on Mac OS X]].
   
== Your first Haskell program ==
+
== Start Haskell ==
   
 
Open a terminal. If you installed GHC type '''ghci''' (the GHC
Please note that this example will not run in hugs, at least not in version 20050308, because hugs does not support defining functions in command prompt. You have to put your definitions into a source file.
 
  +
interpreter). If you installed Hugs type '''hugs'''.
----
 
If you've learned to program another language, your first program probably was Hello world. In Haskell, your first program is the Factorial function.
 
 
'''1) Start an interpreter'''
 
   
Open a terminal. If you installed GHC type '''ghci''' (the GHC interpreter). If you installed Hugs type '''hugs'''.
 
 
<pre>
 
<pre>
 
$ ghci
 
$ ghci
Line 44: Line 42:
 
</pre>
 
</pre>
   
  +
And you are presented with a prompt. The Haskell system now attentively
'''2) Write your first program'''
 
  +
awaits your input.
  +
 
== 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:
   
From GHCi, we can either load programs from files, or we can define them directly at the command promt. Try the following:
 
 
<haskell>
 
<haskell>
 
Prelude> "Hello, World!"
Prelude> let fac n = if n == 0 then 1 else n * fac (n-1)
 
 
"Hello, World!"
 
</haskell>
 
</haskell>
This defines a function called '''fac''' which computes the factorial of an integer (see step 4 for details).
 
   
  +
The Haskell system evaluated the string, and printed the result.
'''3) Run it'''
 
  +
Or we can try a variation to print directly to standard output:
   
Type '''fac 12''' to pass the argument 12 to the function '''fac'''.
 
 
<haskell>
 
<haskell>
  +
Prelude> putStrLn "Hello World"
*Main> fac 12
 
  +
Hello World
479001600
 
 
</haskell>
 
</haskell>
'''Congratulations!''' You've successfully run your first Haskell program.
 
   
  +
Using a Haskell compiler, such as [http://haskell.org/ghc GHC], you can
=== But I want Hello World! ===
 
  +
compile the code to a standalone executable. Create a source file
  +
'''hello.hs''' containing:
  +
  +
<haskell>
 
main = putStrLn "Hello, World!"
  +
</haskell>
  +
  +
And compile it with:
  +
  +
<pre>
  +
$ ghc -o hello hello.hs
  +
</pre>
  +
  +
You can then run the executable ('''./hello''' on Unix systems, '''hello.exe''' on Windows):
  +
  +
<pre>
  +
$ ./hello
  +
Hello, World!
  +
</pre>
  +
  +
== Haskell the calculator ==
   
  +
Let's do something fun. In Haskell, your first true program is the
Before venturing into the long explanation, here is Hello World:
 
  +
factorial function. So back to the interpreter now and let's define it:
   
 
<haskell>
 
<haskell>
 
Prelude> let fac n = if n == 0 then 1 else n * fac (n-1)
putStrLn "Hello World"
 
 
</haskell>
 
</haskell>
   
 
This defines a new function called '''fac''' which computes the
Typing this into GHCi will give you the result you expector at least, the result you should be expecting. If you want an executable that prints the magic string when it is executed, you can put the following in a file called '''hello.hs''':
 
  +
factorial of an integer.
   
  +
We can now run '''fac''' on some argument:
 
<haskell>
 
<haskell>
  +
Prelude> fac 42
main = putStrLn "Hello World"
 
  +
1405006117752879898543142606244511569936384000000000
 
</haskell>
 
</haskell>
   
  +
'''Congratulations!''' Mathematics made easy.
The compiler requires a value called '''main''', so it knows what to execute. Compile it with '''ghc hello.hs -o hello''' and run the resulting executable ('''./hello''' on Unix, '''hello.exe''' on Windows).
 
   
 
----
=== Making a Short Story Long ===
 
   
  +
Note that if you're using Hugs, you'll need to load the definition of
One interesting aspect of functional programming is that we are working with ''functions''. Functions don't have side
 
  +
'''fac''' from a file, '''fac.hs''', containing:
effects, they return a value that depends on the parameters. In order to do IO (say, printing out messages), you could imagine functions taking a parameter '''world''' encompassing all external state, and returning a modified '''world''' (one with the words Hello World on your monitor), which in turn can be passed on to subsequent functions.
 
   
  +
<haskell>
Haskell provides something similar, but more convenient: it treats functions that interact with the world as a separate type, often called ''IO actions''. So while a function converting a number to its printed representation can have type '''Int -> String''', a function reading a string with a specific length from the terminal will probably have type '''Int -> IO String'''. This means that, given an '''Int''', it returns an ''IO action'' for reading a '''String'''. Hopefully, this explains why '''main''' has type '''IO ()''' -- you generally want your program to be able to interact with the world, and thus it must itself be an IO action.
 
  +
fac n = if n == 0 then 1 else n * fac (n-1)
  +
</haskell>
   
  +
And run it with Hugs as follows:
This may sound complicated, but it isn't all that different from other languages that separate ''statements'' from ''expressions'', and the advantage is that Haskell has a solid framework for working with this, namely the '''IO Monad'''.
 
   
  +
<haskell>
If you'd like a quick introduction to how IO works in Haskell, please have a look at [[Introduction to IO]].
 
  +
Hugs.Base> :l fac.hs
 
Main> fac 42
  +
1405006117752879898543142606244511569936384000000000
  +
</haskell>
   
 
== Where to go from here ==
 
== Where to go from here ==
   
There are quite a few good Haskell tutorials and books. Here are some we recommend:
+
There are many good Haskell tutorials and books. Here are some we recommend:
   
 
'''Tutorials'''
 
'''Tutorials'''
Line 118: Line 151:
 
For more, see [[Learning Haskell]] and [[Books and tutorials]].
 
For more, see [[Learning Haskell]] and [[Books and tutorials]].
   
== Get help ==
+
'''Getting help'''
   
 
If you have questions, join the [http://haskell.org/mailman/listinfo/haskell-cafe Haskell-Cafe mailing list] or the [[IRC channel]] and ask. You can also ask questions here on the wiki, see [[Questions and answers]].
 
If you have questions, join the [http://haskell.org/mailman/listinfo/haskell-cafe Haskell-Cafe mailing list] or the [[IRC channel]] and ask. You can also ask questions here on the wiki, see [[Questions and answers]].
 
 
Information about Haskell support for various operating systems is [http://www.haskell.org/haskellwiki/Category:OS here].
 
Information about Haskell support for various operating systems is [http://www.haskell.org/haskellwiki/Category:OS here].
   

Revision as of 06:21, 20 September 2006

Haskell is a general purpose, purely functional programming language. This page will help you get started as quickly as possible.

Install Haskell

Haskell, like most other languages, comes in two flavors: batch oriented (compiler) and interactive (interpreter). An interactive system gives you a command line where you can experiment and evaluate expressions directly, and is probably a good choice to start with.

GHC Compiler and interpreter (GHCi) Probably the most feature-complete system
Hugs Interpreter only Very portable, and more lightweight than GHC.

While both GHC and Hugs work on Windows, Hugs has perhaps the best integration on that platform. There is also information available on installing Haskell software on Mac OS X.

Start Haskell

Open a terminal. If you installed GHC type ghci (the GHC interpreter). If you installed Hugs type hugs.

$ ghci
   ___         ___ _
  / _ \ /\  /\/ __(_)
 / /_\// /_/ / /  | |      GHC Interactive, version 6.4, for Haskell 98.
/ /_\\/ __  / /___| |      http://www.haskell.org/ghc/
\____/\/ /_/\____/|_|      Type :? for help.

Loading package base-1.0 ... linking ... done.
Prelude>

And you are presented with a prompt. The Haskell system now attentively awaits your input.

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!"

The Haskell system evaluated the string, and printed the result. Or we can try a variation to print directly to standard output:

Prelude> putStrLn "Hello World"
Hello World

Using a Haskell compiler, such as GHC, you can compile the code to a standalone executable. Create a source file hello.hs containing:

main = putStrLn "Hello, World!"

And compile it with:

$ ghc -o hello hello.hs

You can then run the executable (./hello on Unix systems, hello.exe on Windows):

$ ./hello
Hello, World!

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)

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

We can now run fac on some argument:

Prelude> fac 42
1405006117752879898543142606244511569936384000000000

Congratulations! Mathematics made easy.


Note that if you're using Hugs, you'll need to load the definition of fac from a file, fac.hs, containing:

fac n = if n == 0 then 1 else n * fac (n-1)

And run it with Hugs as follows:

Hugs.Base> :l fac.hs
Main> fac 42
1405006117752879898543142606244511569936384000000000

Where to go from here

There are many good Haskell tutorials and books. Here are some we recommend:

Tutorials

Courses

References

Textbooks

For more, see Learning Haskell and Books and tutorials.

Getting help

If you have questions, join the Haskell-Cafe mailing list or the IRC channel and ask. You can also ask questions here on the wiki, see Questions and answers. Information about Haskell support for various operating systems is here.