Difference between revisions of "Cn/Haskell in 5 steps"

From HaskellWiki
Jump to navigation Jump to search
(move to cn/Haskell 五步走)
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
<!--
 
Haskell is a general purpose, purely functional programming language.
 
This page will help you get started as quickly as possible.
 
-->
 
Haskell 是一门通用型纯粹函数式编程语言。
 
此篇将引导你尽快上路。
 
 
__TOC__
 
 
== 安装 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.
 
-->
 
类似其他语言,Haskell 提供了两种风格的处理方式:批处理(“编译器”)和交互式(“解释器”)。
 
其中解释器可直接用于测试和计算,是初学者的好帮手。
 
 
<!--
 
{| 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.
 
|}
 
-->
 
{| class="wikitable"
 
|[http://www.haskell.org/ghc/ GHC]
 
|编译器和解释器(GHCi)
 
|可能是特性实现最全面的系统
 
|-
 
|[http://www.haskell.org/hugs/ Hugs]
 
|解释器
 
|高度可移植,轻量级
 
|}
 
 
<!--
 
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]].
 
-->
 
GHC 和 Hugs 都可以在 Windows 上工作,Hugs 似乎和此平台配合得更好。
 
[[Mac OS X|在 Mac OS X 上安装 Haskell]]。
 
 
<!--
 
== Start Haskell ==
 
-->
 
== 运行 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'''.
 
-->
 
启动一个终端。
 
如果安装了 GHC,在命令行输入 '''ghci''' (GHC 解释器的可执行文件名)。
 
如果安装了 Hugs,输入 '''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.
 
-->
 
现在你看到一个提示符,Haskell 系统期候你的输入。
 
 
<!--
 
== Write your first Haskell program ==
 
-->
 
== 第一个 Haskell 程序 ==
 
 
<!--
 
If you've learned to program another language, your first program
 
probably was "Hello, world!", so let's do that:
 
-->
 
如果学习过其他编程语言,你的第一个程序可能就是 "Hello, world!"。我们可以这样:
 
 
<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 系统对此字符串求值,并输出结果。
 
也可以修改一下,以直接输出到标准输出:
 
 
 
<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 编译器,如 [http://haskell.org/ghc GHC],可以把代码编译成单独的可执行档。
 
新建源文件 '''hello.hs''',包含:
 
 
 
<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):
 
-->
 
运行之(Unix 系统上用 '''./hello''',Windows 系统用 '''hello.exe'''):
 
 
<pre>
 
$ ./hello
 
Hello, World!
 
</pre>
 
 
<!--
 
== Haskell the calculator ==
 
-->
 
== Haskell 计算器 ==
 
 
 
<!--
 
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:
 
-->
 
Let's do something fun. 你的第一个真正的程序是阶乘函数。回到解释器,定义之:
 
 
<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!'''
 
 
== 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]])
 
 
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 in 5 steps|zh/cn]]
 

Latest revision as of 06:14, 15 December 2007