Difference between revisions of "Ja/Haskell入門 5ステップ"

From HaskellWiki
Jump to navigation Jump to search
 
 
Line 1: Line 1:
Haskell is a general purpose, purely functional programming language.
 
This page will help you get started as quickly as possible.
 
 
__TOC__
 
 
== 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.
 
 
{| class="wikitable"
 
|[http://www.haskell.org/ghc/ GHC]
 
|Compiler and interpreter (GHCi)
 
|Probably the most feature-complete system
 
|-
 
|[http://www.haskell.org/hugs/ 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. Nonetheless, GHC is more actively developed and maintained, and the consensus seems to be that it is the generally-recommended environment of choice for newcomers to Haskell as well as old hands. There is also information available on
 
[[Mac OS X|installing Haskell software on Mac OS X]].
 
 
== Start Haskell ==
 
 
Open a terminal. If you installed GHC, type '''ghci''' (the name of the executable of the GHC
 
interpreter) at the command prompt. If you installed Hugs, type '''hugs'''.
 
 
<pre>
 
$ 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>
 
</pre>
 
 
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:
 
 
<haskell>
 
Prelude> "Hello, World!"
 
"Hello, World!"
 
</haskell>
 
 
The Haskell system evaluated the string, and printed the result.
 
Or we can try a variation to print directly to standard output:
 
 
<haskell>
 
Prelude> putStrLn "Hello World"
 
Hello World
 
</haskell>
 
 
Using a Haskell compiler, such as [http://haskell.org/ghc GHC], you can
 
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
 
factorial function. So back to the interpreter now and let's define it:
 
 
<haskell>
 
Prelude> let fac n = if n == 0 then 1 else n * fac (n-1)
 
</haskell>
 
 
This defines a new function called '''fac''' which computes the
 
factorial of an integer.
 
 
We can now run '''fac''' on some argument:
 
<haskell>
 
Prelude> fac 42
 
1405006117752879898543142606244511569936384000000000
 
</haskell>
 
 
'''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:
 
 
<haskell>
 
fac n = if n == 0 then 1 else n * fac (n-1)
 
</haskell>
 
 
And run it with Hugs as follows (this also works in GHCi):
 
 
<haskell>
 
Hugs.Base> :load fac.hs
 
Main> fac 42
 
1405006117752879898543142606244511569936384000000000
 
</haskell>
 
 
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):
 
 
<haskell>
 
fac 0 = 1
 
fac n = n * fac (n-1)
 
 
main = print (fac 42)
 
</haskell>
 
 
which can then be compiled and run:
 
 
<pre>
 
$ ghc -o fac fac.hs
 
$ ./fac
 
1405006117752879898543142606244511569936384000000000
 
</pre>
 
 
'''Great!'''
 
 
===Write your first parallel Haskell program===
 
 
Haskell has good support for parallel and multicore programming. We can write a parallel program by adding `par` to expressions, like so:
 
 
<haskell>
 
import Control.Parallel
 
 
main = a `par` b `par` c `pseq` print (a + b + c)
 
where
 
a = ack 3 10
 
b = fac 42
 
c = fib 34
 
 
fac 0 = 1
 
fac n = n * fac (n-1)
 
 
ack 0 n = n+1
 
ack m 0 = ack (m-1) 1
 
ack m n = ack (m-1) (ack m (n-1))
 
 
fib 0 = 0
 
fib 1 = 1
 
fib n = fib (n-1) + fib (n-2)
 
</haskell>
 
 
Compiling with -threaded and optimizations on:
 
 
<pre>
 
$ ghc -O2 --make A.hs -threaded
 
[1 of 1] Compiling Main ( A.hs, A.o )
 
Linking A ...
 
</pre>
 
 
And now we can run our multicore program. Here across two cores:
 
 
<pre>
 
$ time ./A +RTS -N2
 
1405006117752879898543142606244511569936384005711076
 
./A +RTS -N2 2.14s user 0.02s system 149% cpu 1.449 total
 
</pre>
 
 
Congratulations! You are now programming your multicore!
 
 
== Where to go from here ==
 
 
There are many good Haskell tutorials and books. Here are some we recommend:
 
 
'''Tutorials'''
 
* [[Learn_Haskell_in_10_minutes|Haskell in 10 minutes]]
 
* [http://darcs.haskell.org/yaht/yaht.pdf Yet Another Haskell Tutorial] (English)
 
* [http://www.haskell.org/tutorial/ A Gentle Introduction to Haskell] (English, [[Image:GentleFR.pdf|French PDF]])
 
* [http://learnyouahaskell.com/ Learn You A Haskell For Great Good!]
 
 
For a complete list of textbooks, references and tutorials:
 
 
* [[Books and tutorials]]
 
 
'''Join the community!'''
 
 
Talk to others in the Haskell community:
 
 
* [http://haskell.org/mailman/listinfo/haskell-cafe Haskell-Cafe mailing list]
 
* [[IRC channel]]
 
 
[[Category:Tutorials]]
 
Languages: [[5 adımda Haskell|tur]] [[Cn/Haskell 入门五步走|zh/cn]] [[Tw/Haskell入門的5個步驟|zh/tw]] [[Ja/Haskell入門 5ステップ|ja]]
 

Latest revision as of 15:23, 3 December 2009