Difference between revisions of "Haskell in 5 steps"

From HaskellWiki
Jump to navigation Jump to search
(point to haskell.es)
(simplify, and point to common tutorial/book page)
Line 145: Line 145:
 
'''Tutorials'''
 
'''Tutorials'''
 
* [http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf Yet Another Haskell Tutorial] (English)
 
* [http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf Yet Another Haskell Tutorial] (English)
* [ftp://ftp.geoinfo.tuwien.ac.at/navratil/HaskellTutorial.pdf Haskell-Tutorial] (English)
 
 
* [http://www.haskell.org/tutorial/ A Gentle Introduction to Haskell] (English)
 
* [http://www.haskell.org/tutorial/ A Gentle Introduction to Haskell] (English)
* [http://www.informatik.uni-bonn.de/~ralf/teaching/Hskurs_toc.html Haskell Kurs] (Deutsch)
 
* [[Haskell.es]] (Spanish)
 
   
  +
For a complete list of textbooks, references and tutorials:
'''Courses'''
 
* [http://www.cs.chalmers.se/Cs/Grundutb/Kurser/d1pt/d1pta/external.html Programming in Haskell] (English)
 
* Functional programming ([http://www.cs.uu.nl/~jeroen/courses/fp-eng.pdf English], [http://www.cs.uu.nl/~jeroen/courses/fp-sp.pdf Español], [http://www.cs.uu.nl/~jeroen/courses/fp-nl.pdf Netherlands]) (Note, that this uses an old version of Haskell; e.g. input/output has changed since then)
 
   
  +
* [[Books and tutorials]].
'''References'''
 
* [http://www.cs.uu.nl/~afie/haskell/tourofsyntax.html Tour of Haskell Syntax] ([http://cs.anu.edu.au/Student/comp1100/haskell/tourofsyntax.html this link seems to work])
 
* [http://zvon.org/other/haskell/Outputglobal/index.html Haskell Reference]
 
* [http://www.cs.uu.nl/~afie/haskell/tourofprelude.html Tour of the Haskell Prelude] ([http://www.comp.nus.edu.sg/~sulzmann/courses/melbourne/sem2002-1/141/www/tourofprelude.html this link seems to work])
 
* [http://members.chello.nl/hjgtuyl/tourdemonad.html A tour of the Haskell Monad functions]
 
   
'''Textbooks'''
+
'''Join the community!'''
* [http://www.haskell.org/soe The Haskell School of Expression]
 
* [http://www.cs.ukc.ac.uk/people/staff/sjt/craft2e/ Haskell: The Craft of Functional Programming]
 
* [http://www.prenhall.com/allbooks/ptr_0134843460.html Introduction to Functional Programming using Haskell]
 
* [http://books.cambridge.org/0521277248.htm An Introduction to Functional Programming Systems Using Haskell]
 
* [http://www.iro.umontreal.ca/~lapalme/Algorithms-functional.html Algorithms: A functional programming approach]
 
* [http://homepages.cwi.nl/~jve/HR/ The Haskell Road to Logic, Maths and Programming]
 
   
  +
Talk to others in the Haskell community:
For more, see [[Learning Haskell]] and [[Books and tutorials]].
 
   
  +
* [http://haskell.org/mailman/listinfo/haskell-cafe Haskell-Cafe mailing list]
'''Getting help'''
 
  +
* [[IRC channel]]
 
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].
 
   
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]

Revision as of 01:32, 3 November 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! 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:

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

And run it with Hugs as follows (this also works in GHCi):

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

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

fac 0 = 1
fac n = n * fac (n-1)

main = print (fac 42)

which can then be compiled and run:

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

Great!

Where to go from here

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

Tutorials

For a complete list of textbooks, references and tutorials:

Join the community!

Talk to others in the Haskell community: