Difference between revisions of "Haskell in 5 steps"

From HaskellWiki
Jump to navigation Jump to search
(link)
(syntax)
Line 4: Line 4:
   
 
== Why learn Haskell? ==
 
== Why learn Haskell? ==
  +
 
John Hughes has written a good essay: [http://www.md.chalmers.se/~rjmh/Papers/whyfp.html Why Functional Programming Matters].
 
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.
   
 
{| class="wikitable"
 
{| class="wikitable"
Line 20: Line 22:
   
 
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 ==
 
== 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.
 
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 43: Line 47:
   
 
From GHCi, we can either load programs from files, or we can define them directly at the command promt. Try the following:
 
From GHCi, we can either load programs from files, or we can define them directly at the command promt. Try the following:
<pre>
+
<haskell>
 
Prelude> let fac n = if n == 0 then 1 else n * fac (n-1)
 
Prelude> let fac n = if n == 0 then 1 else n * fac (n-1)
</pre>
+
</haskell>
 
This defines a function called '''fac''' which computes the factorial of an integer (see step 4 for details).
 
This defines a function called '''fac''' which computes the factorial of an integer (see step 4 for details).
   
Line 51: Line 55:
   
 
Type '''fac 12''' to pass the argument 12 to the function '''fac'''.
 
Type '''fac 12''' to pass the argument 12 to the function '''fac'''.
<pre>
+
<haskell>
 
*Main> fac 12
 
*Main> fac 12
 
479001600
 
479001600
</pre>
+
</haskell>
 
'''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>
+
<haskell>
 
putStrLn "Hello World"
 
putStrLn "Hello World"
</pre>
+
</haskell>
   
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 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''':
 
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''':
   
<pre>
+
<haskell>
module Main where
 
 
 
main = putStrLn "Hello World"
 
main = putStrLn "Hello World"
</pre>
+
</haskell>
   
 
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).
 
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).
Line 80: Line 82:
   
 
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.
Line 89: Line 91:
   
 
== 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 quite a few good Haskell tutorials and books. Here are some we recommend:
   
Line 118: Line 121:
   
 
== Get help ==
 
== Get 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]].
   

Revision as of 08:42, 17 September 2006

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

Why learn Haskell?

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

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. There is also information available on installing Haskell software on Mac OS X.

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.

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

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

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.

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 Haskell and Books and tutorials.

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.

Information about Haskell support for various operating systems is here.