https://wiki.haskell.org/api.php?action=feedcontributions&user=Peter.arrenbrecht&feedformat=atomHaskellWiki - User contributions [en]2024-03-28T23:57:53ZUser contributionsMediaWiki 1.35.5https://wiki.haskell.org/index.php?title=Haskell_in_5_steps&diff=6278Haskell in 5 steps2006-09-26T14:36:10Z<p>Peter.arrenbrecht: Another alternative link</p>
<hr />
<div>Haskell is a general purpose, purely functional programming language.<br />
This page will help you get started as quickly as possible.<br />
<br />
__TOC__<br />
<br />
== Install Haskell ==<br />
<br />
Haskell, like most other languages, comes in two flavors: batch oriented<br />
(''compiler'') and interactive (''interpreter''). An interactive system<br />
gives you a command line where you can experiment and evaluate<br />
expressions directly, and is probably a good choice to start with.<br />
<br />
{| class="wikitable"<br />
|[http://www.haskell.org/ghc/ GHC]<br />
|Compiler and interpreter (GHCi)<br />
|Probably the most feature-complete system<br />
|-<br />
|[http://www.haskell.org/hugs/ Hugs]<br />
|Interpreter only<br />
|Very portable, and more lightweight than GHC.<br />
|}<br />
<br />
While both GHC and Hugs work on Windows, Hugs has perhaps the best<br />
integration on that platform. There is also information available on<br />
[[Mac OS X|installing Haskell software on Mac OS X]].<br />
<br />
== Start Haskell ==<br />
<br />
Open a terminal. If you installed GHC type '''ghci''' (the GHC<br />
interpreter). If you installed Hugs type '''hugs'''. <br />
<br />
<pre><br />
$ ghci<br />
___ ___ _<br />
/ _ \ /\ /\/ __(_)<br />
/ /_\// /_/ / / | | GHC Interactive, version 6.4, for Haskell 98.<br />
/ /_\\/ __ / /___| | http://www.haskell.org/ghc/<br />
\____/\/ /_/\____/|_| Type :? for help.<br />
<br />
Loading package base-1.0 ... linking ... done.<br />
Prelude><br />
</pre><br />
<br />
And you are presented with a prompt. The Haskell system now attentively<br />
awaits your input.<br />
<br />
== Write your first Haskell program ==<br />
<br />
If you've learned to program another language, your first program<br />
probably was "Hello, world!", so let's do that:<br />
<br />
<haskell><br />
Prelude> "Hello, World!"<br />
"Hello, World!"<br />
</haskell><br />
<br />
The Haskell system evaluated the string, and printed the result. <br />
Or we can try a variation to print directly to standard output:<br />
<br />
<haskell><br />
Prelude> putStrLn "Hello World"<br />
Hello World<br />
</haskell><br />
<br />
Using a Haskell compiler, such as [http://haskell.org/ghc GHC], you can<br />
compile the code to a standalone executable. Create a source file<br />
'''hello.hs''' containing:<br />
<br />
<haskell><br />
main = putStrLn "Hello, World!"<br />
</haskell><br />
<br />
And compile it with:<br />
<br />
<pre><br />
$ ghc -o hello hello.hs<br />
</pre><br />
<br />
You can then run the executable ('''./hello''' on Unix systems, '''hello.exe''' on Windows):<br />
<br />
<pre><br />
$ ./hello<br />
Hello, World!<br />
</pre><br />
<br />
== Haskell the calculator ==<br />
<br />
Let's do something fun. In Haskell, your first true program is the<br />
factorial function. So back to the interpreter now and let's define it:<br />
<br />
<haskell><br />
Prelude> let fac n = if n == 0 then 1 else n * fac (n-1)<br />
</haskell><br />
<br />
This defines a new function called '''fac''' which computes the<br />
factorial of an integer.<br />
<br />
We can now run '''fac''' on some argument:<br />
<haskell><br />
Prelude> fac 42<br />
1405006117752879898543142606244511569936384000000000<br />
</haskell><br />
<br />
'''Congratulations!''' Programming made easy. Note that if you're using<br />
Hugs, you'll need to load the definition of '''fac''' from a file,<br />
'''fac.hs''', containing:<br />
<br />
<haskell><br />
fac n = if n == 0 then 1 else n * fac (n-1)<br />
</haskell><br />
<br />
And run it with Hugs as follows (this also works in GHCi):<br />
<br />
<haskell><br />
Hugs.Base> :load fac.hs<br />
Main> fac 42<br />
1405006117752879898543142606244511569936384000000000<br />
</haskell><br />
<br />
We can of course compile this program, to produce a standalone<br />
executable. In the file '''fac.hs''' we can write (and let's use<br />
elegant pattern matching syntax just for fun):<br />
<br />
<haskell><br />
fac 0 = 1<br />
fac n = n * fac (n-1)<br />
<br />
main = print (fac 42)<br />
</haskell><br />
<br />
which can then be compiled and run:<br />
<br />
<pre><br />
$ ghc -o fac fac.hs<br />
$ ./fac<br />
1405006117752879898543142606244511569936384000000000<br />
</pre><br />
<br />
'''Great!'''<br />
<br />
== Where to go from here ==<br />
<br />
There are many good Haskell tutorials and books. Here are some we recommend:<br />
<br />
'''Tutorials'''<br />
* [http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf Yet Another Haskell Tutorial] (English)<br />
* [ftp://ftp.geoinfo.tuwien.ac.at/navratil/HaskellTutorial.pdf Haskell-Tutorial] (English)<br />
* [http://www.haskell.org/tutorial/ A Gentle Introduction to Haskell] (English)<br />
* [http://www.informatik.uni-bonn.de/~ralf/teaching/Hskurs_toc.html Haskell Kurs] (Deutsch)<br />
<br />
'''Courses'''<br />
* [http://www.cs.chalmers.se/Cs/Grundutb/Kurser/d1pt/d1pta/external.html Programming in Haskell] (English)<br />
* 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)<br />
<br />
'''References'''<br />
* [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])<br />
* [http://zvon.org/other/haskell/Outputglobal/index.html Haskell Reference]<br />
* [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])<br />
* [http://members.chello.nl/hjgtuyl/tourdemonad.html A tour of the Haskell Monad functions]<br />
<br />
'''Textbooks'''<br />
* [http://www.haskell.org/soe The Haskell School of Expression]<br />
* [http://www.cs.ukc.ac.uk/people/staff/sjt/craft2e/ Haskell: The Craft of Functional Programming]<br />
* [http://www.prenhall.com/allbooks/ptr_0134843460.html Introduction to Functional Programming using Haskell]<br />
* [http://books.cambridge.org/0521277248.htm An Introduction to Functional Programming Systems Using Haskell]<br />
* [http://www.iro.umontreal.ca/~lapalme/Algorithms-functional.html Algorithms: A functional programming approach]<br />
* [http://homepages.cwi.nl/~jve/HR/ The Haskell Road to Logic, Maths and Programming]<br />
<br />
For more, see [[Learning Haskell]] and [[Books and tutorials]].<br />
<br />
'''Getting help'''<br />
<br />
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]].<br />
Information about Haskell support for various operating systems is [http://www.haskell.org/haskellwiki/Category:OS here].<br />
<br />
[[Category:Tutorials]]</div>Peter.arrenbrechthttps://wiki.haskell.org/index.php?title=Haskell_in_5_steps&diff=6277Haskell in 5 steps2006-09-26T14:34:30Z<p>Peter.arrenbrecht: Added alternative link for broken one</p>
<hr />
<div>Haskell is a general purpose, purely functional programming language.<br />
This page will help you get started as quickly as possible.<br />
<br />
__TOC__<br />
<br />
== Install Haskell ==<br />
<br />
Haskell, like most other languages, comes in two flavors: batch oriented<br />
(''compiler'') and interactive (''interpreter''). An interactive system<br />
gives you a command line where you can experiment and evaluate<br />
expressions directly, and is probably a good choice to start with.<br />
<br />
{| class="wikitable"<br />
|[http://www.haskell.org/ghc/ GHC]<br />
|Compiler and interpreter (GHCi)<br />
|Probably the most feature-complete system<br />
|-<br />
|[http://www.haskell.org/hugs/ Hugs]<br />
|Interpreter only<br />
|Very portable, and more lightweight than GHC.<br />
|}<br />
<br />
While both GHC and Hugs work on Windows, Hugs has perhaps the best<br />
integration on that platform. There is also information available on<br />
[[Mac OS X|installing Haskell software on Mac OS X]].<br />
<br />
== Start Haskell ==<br />
<br />
Open a terminal. If you installed GHC type '''ghci''' (the GHC<br />
interpreter). If you installed Hugs type '''hugs'''. <br />
<br />
<pre><br />
$ ghci<br />
___ ___ _<br />
/ _ \ /\ /\/ __(_)<br />
/ /_\// /_/ / / | | GHC Interactive, version 6.4, for Haskell 98.<br />
/ /_\\/ __ / /___| | http://www.haskell.org/ghc/<br />
\____/\/ /_/\____/|_| Type :? for help.<br />
<br />
Loading package base-1.0 ... linking ... done.<br />
Prelude><br />
</pre><br />
<br />
And you are presented with a prompt. The Haskell system now attentively<br />
awaits your input.<br />
<br />
== Write your first Haskell program ==<br />
<br />
If you've learned to program another language, your first program<br />
probably was "Hello, world!", so let's do that:<br />
<br />
<haskell><br />
Prelude> "Hello, World!"<br />
"Hello, World!"<br />
</haskell><br />
<br />
The Haskell system evaluated the string, and printed the result. <br />
Or we can try a variation to print directly to standard output:<br />
<br />
<haskell><br />
Prelude> putStrLn "Hello World"<br />
Hello World<br />
</haskell><br />
<br />
Using a Haskell compiler, such as [http://haskell.org/ghc GHC], you can<br />
compile the code to a standalone executable. Create a source file<br />
'''hello.hs''' containing:<br />
<br />
<haskell><br />
main = putStrLn "Hello, World!"<br />
</haskell><br />
<br />
And compile it with:<br />
<br />
<pre><br />
$ ghc -o hello hello.hs<br />
</pre><br />
<br />
You can then run the executable ('''./hello''' on Unix systems, '''hello.exe''' on Windows):<br />
<br />
<pre><br />
$ ./hello<br />
Hello, World!<br />
</pre><br />
<br />
== Haskell the calculator ==<br />
<br />
Let's do something fun. In Haskell, your first true program is the<br />
factorial function. So back to the interpreter now and let's define it:<br />
<br />
<haskell><br />
Prelude> let fac n = if n == 0 then 1 else n * fac (n-1)<br />
</haskell><br />
<br />
This defines a new function called '''fac''' which computes the<br />
factorial of an integer.<br />
<br />
We can now run '''fac''' on some argument:<br />
<haskell><br />
Prelude> fac 42<br />
1405006117752879898543142606244511569936384000000000<br />
</haskell><br />
<br />
'''Congratulations!''' Programming made easy. Note that if you're using<br />
Hugs, you'll need to load the definition of '''fac''' from a file,<br />
'''fac.hs''', containing:<br />
<br />
<haskell><br />
fac n = if n == 0 then 1 else n * fac (n-1)<br />
</haskell><br />
<br />
And run it with Hugs as follows (this also works in GHCi):<br />
<br />
<haskell><br />
Hugs.Base> :load fac.hs<br />
Main> fac 42<br />
1405006117752879898543142606244511569936384000000000<br />
</haskell><br />
<br />
We can of course compile this program, to produce a standalone<br />
executable. In the file '''fac.hs''' we can write (and let's use<br />
elegant pattern matching syntax just for fun):<br />
<br />
<haskell><br />
fac 0 = 1<br />
fac n = n * fac (n-1)<br />
<br />
main = print (fac 42)<br />
</haskell><br />
<br />
which can then be compiled and run:<br />
<br />
<pre><br />
$ ghc -o fac fac.hs<br />
$ ./fac<br />
1405006117752879898543142606244511569936384000000000<br />
</pre><br />
<br />
'''Great!'''<br />
<br />
== Where to go from here ==<br />
<br />
There are many good Haskell tutorials and books. Here are some we recommend:<br />
<br />
'''Tutorials'''<br />
* [http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf Yet Another Haskell Tutorial] (English)<br />
* [ftp://ftp.geoinfo.tuwien.ac.at/navratil/HaskellTutorial.pdf Haskell-Tutorial] (English)<br />
* [http://www.haskell.org/tutorial/ A Gentle Introduction to Haskell] (English)<br />
* [http://www.informatik.uni-bonn.de/~ralf/teaching/Hskurs_toc.html Haskell Kurs] (Deutsch)<br />
<br />
'''Courses'''<br />
* [http://www.cs.chalmers.se/Cs/Grundutb/Kurser/d1pt/d1pta/external.html Programming in Haskell] (English)<br />
* 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)<br />
<br />
'''References'''<br />
* [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])<br />
* [http://zvon.org/other/haskell/Outputglobal/index.html Haskell Reference]<br />
* [http://www.cs.uu.nl/~afie/haskell/tourofprelude.html Tour of the Haskell Prelude]<br />
* [http://members.chello.nl/hjgtuyl/tourdemonad.html A tour of the Haskell Monad functions]<br />
<br />
'''Textbooks'''<br />
* [http://www.haskell.org/soe The Haskell School of Expression]<br />
* [http://www.cs.ukc.ac.uk/people/staff/sjt/craft2e/ Haskell: The Craft of Functional Programming]<br />
* [http://www.prenhall.com/allbooks/ptr_0134843460.html Introduction to Functional Programming using Haskell]<br />
* [http://books.cambridge.org/0521277248.htm An Introduction to Functional Programming Systems Using Haskell]<br />
* [http://www.iro.umontreal.ca/~lapalme/Algorithms-functional.html Algorithms: A functional programming approach]<br />
* [http://homepages.cwi.nl/~jve/HR/ The Haskell Road to Logic, Maths and Programming]<br />
<br />
For more, see [[Learning Haskell]] and [[Books and tutorials]].<br />
<br />
'''Getting help'''<br />
<br />
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]].<br />
Information about Haskell support for various operating systems is [http://www.haskell.org/haskellwiki/Category:OS here].<br />
<br />
[[Category:Tutorials]]</div>Peter.arrenbrecht