Haskell基础知识
Table of Contents
1 Haskell 基础知识
1.1 Haskell 开发环境安装
1.2 Haskell 代码如何运行
- 代码
-- helloWorld.hs
-- 定义名为hi的函数
hi = "hello, world!"
- 运行
$ ghci
GHCi, version 7.4.1: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
ghci>:l helloWorld.hs
[1 of 1] Compiling Main ( helloWorld.hs, interpreted )
Ok, modules loaded: Main.
ghci>hi
"hello, world!"
"hello, world!"
1.3 Haskell 函数定义和使用
-- function.hs
doubleMe x = x + x
doubleUs x y = doubleMe x + doubleMe y
ghci>:l function.hs
[1 of 1] Compiling Main ( function.hs, interpreted )
Ok, modules loaded: Main.
ghci>doubleMe 2
4
ghci>doubleUs 3 4
14
1.4 Haskell 基本语句和编程模式
- if语句
maxNumber x y = if x > y then x
else y
ghci>:l baseStruc.hs
[1 of 1] Compiling Main ( baseStruc.hs, interpreted )
Ok, modules loaded: Main.
ghci>maxNumber 3 5
5
ghci>maxNumber 5 3
5
- case语句
isLucky x = case x of 1 -> "1:lucky"
2 -> "2:No lucky"
otherwise -> "lucky"
ghci>isLucky 1
"1:lucky"
ghci>isLucky 2
"2:No lucky"
ghci>isLucky 3
"lucky"
ghci>isLucky 4
"lucky"
"1:lucky"
ghci>isLucky 2
"2:No lucky"
ghci>isLucky 3
"lucky"
ghci>isLucky 4
"lucky"
- let语句
doubleSum x y =
let doublex = 2 * x
doubley = 2 * y
in doublex + doubley
ghci>doubleSum 3 4
14
ghci>doubleSum 1 2
6
- where语句
doubleSum2 x y = doublex + doubley
where doublex = 2 * x
doubley = 2 * y
ghci>doubleSum2 1 2
6
ghci>doubleSum2 2 3
10
- 模式匹配(Pattern Matching)
例1
lucky 7 = "You are lucky"
lucky x = "Sorry, you are not lucky"
ghci>lucky 7
"You are lucky"
ghci>lucky 1
"Sorry, you are not lucky"
ghci>lucky 2
"Sorry, you are not lucky"
"You are lucky"
ghci>lucky 1
"Sorry, you are not lucky"
ghci>lucky 2
"Sorry, you are not lucky"
例2
foo 0 x = (-1) * x
foo 1 x = x
foo x y = 0
ghci>foo 1 2
2
ghci>foo 2 3
0
ghci>foo 3 4
0
ghci>foo 0 2
-2
例3
foo' 0 x = (-1) * x
foo' x y = 0
foo' 1 x = x
ghci>foo' 1 2
0
ghci>foo' 1 3
0
ghci>foo' 2 3
0
ghci>foo' 3 4
0
ghci>foo' 0 2
-2
- 门卫(Guard)
例1
mdivide x y
| y == 0 = "Can not divide"
| x / y > 10 = "first number is larger than the second number"
| x / y < 1 = "first number is less than the second number"
| otherwise = "almost equal"
ghci>mdivide 3 0
"Can not divide"
ghci>mdivide 3 2
"almost equal"
ghci>mdivide 30 2
"first number is larger than the second number"
ghci>mdivide 3 5
"first number is less than the second number"
"Can not divide"
ghci>mdivide 3 2
"almost equal"
ghci>mdivide 30 2
"first number is larger than the second number"
ghci>mdivide 3 5
"first number is less than the second number"
例2
mdivide' x y
| x / y > 10 = "first number is larger than the second number"
| y == 0 = "Can not divide"
| x / y < 1 = "first number is less than the second number"
| otherwise = "almost equal"
ghci>mdivide' 3 0
"first number is larger than the second number"
ghci>mdivide' 3 1
"almost equal"
ghci>mdivide' 30 3
"almost equal"
ghci>mdivide' 30 2
"first number is larger than the second number"
ghci>mdivide' 2 3
"first number is less than the second number"
"first number is larger than the second number"
ghci>mdivide' 3 1
"almost equal"
ghci>mdivide' 30 3
"almost equal"
ghci>mdivide' 30 2
"first number is larger than the second number"
ghci>mdivide' 2 3
"first number is less than the second number"
- 递归(Recursion)
注:理解门卫与递归如何结合在一起工作
msum x y
| x > y = msum y x
| x == y = x
| otherwise = x + msum (x+1) y
ghci>msum 1 100
5050
ghci>msum 2 4
9
ghci>msum 100 1
5050