Personal tools

Haskell in 5 steps

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
m (removed the “Step [1-5]” from the headlines)
m (fixed punctuation)
Line 7: Line 7:
   
 
== 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.
   
 
{| class="wikitable"
 
{| class="wikitable"
Line 23: Line 23:
 
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.
 
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.
 
----
 
----
If you've learned to program another language, your first program probably was "Hello world". In Haskell, your first program is the Factorial function.
+
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'''
 
'''1) Start an interpreter'''
Line 57: Line 57:
 
'''Congratulations!''' You've successfully run your first Haskell program.
 
'''Congratulations!''' You've successfully run your first Haskell program.
   
=== But I want "Hello World"! ===
+
=== But I want “Hello World”! ===
   
Before venturing into the long explanation, here is "Hello World":
+
Before venturing into the long explanation, here is “Hello World”:
   
 
<pre>
 
<pre>
Line 65: Line 65:
 
</pre>
 
</pre>
   
Typing this into GHCi will give you the result you expect - or at least, the result you should be expecting.
+
Typing this into GHCi will give you the result you expect—or 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''':
 
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''':
Line 80: Line 80:
   
 
One interesting aspect of functional programming is that we are working with ''functions''. Functions don't have side
 
One interesting aspect of functional programming is that we are working with ''functions''. Functions don't have side
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.
+
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 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.
 
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.

Revision as of 21:54, 21 August 2006

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

Contents


1 Why learn Haskell?

John Hughes has written a good essay: Why Functional Programming Matters.

2 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.

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. For installation on Mac OSX see here.

3 Your first Haskell program

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.


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.

$ 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>

2) Write your first program

From GHCi, we can either load programs from files, or we can define them directly at the command promt. Try the following:

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

This defines a function called fac which computes the factorial of an integer (see step 4 for details).

3) Run it

Type fac 12 to pass the argument 12 to the function fac.

*Main> fac 12
479001600

Congratulations! You've successfully run your first Haskell program.

3.1 But I want “Hello World”!

Before venturing into the long explanation, here is “Hello World”:

putStrLn "Hello World"

Typing this into GHCi will give you the result you expect—or 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:

module Main where

main = putStrLn "Hello World"

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).

3.2 Making a Short Story Long

One interesting aspect of functional programming is that we are working with functions. Functions don't have side 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 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.

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.

If you'd like a quick introduction to how IO works in Haskell, please have a look at Introduction to IO.

4 Where to go from here

There are quite a few good Haskell tutorials and books. Here are some we recommend:

Tutorials

Courses

References

Textbooks

For more, see Learning and Books and tutorials.

5 Get 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.