Haskell入门
基本语法
函数
打开编辑器,输入内容,保存为fun.hs
1
2
3doubleMe x = x + x
doubleUs x y = doubleMe x + doubleMe y
doubleSmallNumber' x = (if x>100 then x else x+x) + 1
打开命令行,执行:1
2
3
4
5
6
7
8
9
10
11
12ghci> :l fun
ghci> doubleMe 9
9
ghci>doubleUs 10 20
60
ghci>doubleSmallNumber' 10
21
ghci>dou
ghci>doubleSmallNumber' 101
102
列表
拼接,使用++
1 | ghci>[1,2,3] ++ [4,5,6] |
“Hello”等价于[‘H’,’e’,’l’,’l’,’o’]
在头部插入,使用:
1 | ghci>'H':"ello" |
访问列表,使用!!
1 | ghci>"Hello"!!2 |
1 | ghci>5 `elem` |
比较列表,使用>
,<
,<=
,>=
1
2
3ghci>[3,2,1] >
False
其他操作1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41ghci>head
5
ghci>tail
ghci>last
1
ghci>init
ghci>length
5
ghci>null
False
ghci>null
True
ghci>reverse
ghci>take 3
ghci>take 0
ghci>drop 3
ghci>drop 100
ghci>maximum
5
ghci>minimum
1
ghci>sum
15
ghci>product
120
ghci>5 `elem`
True
ghci>6 `elem`
False
ghci>elem 5
True
ghci>elem 6
False
区间
1 | ghci>[1..30] |
列表推导
1 | ghci>[x*2|x<-[1..10]] |
元组
1 | ghci>fst (1,"hello") |
模式匹配
简单模式匹配
1 | lucky :: Int -> String |
由上到下进行匹配,x
为匹配任何值,执行结果如下:1
2
3
4ghci>lucky 7
"LUCKY NUMBER SEVEN"
ghci>lucky 9
"Default Hello!"
阶乘计算:1
2
3factorial :: Int -> Int
factorial 0 = 1
factorial n = n*(factorial (n-1))
执行结果:1
2ghci>factorial 10
3628800
元组模式匹配1
2addVector :: (Double,Double) -> (Double,Double) -> (Double,Double)
addVector (x1,y1) (x2,y2) = (x1+x2,y1+y2)
执行结果:1
2ghci>addVector (1,2) (3,4)
(4.0,6.0)
列表模式匹配1
2
3head' :: [a]->a
head' [] = error "list can't be empty"
head' (x:_) = x
执行结果:
1 | ghci>head' [1,2,3,4] |
匹配时使用@
引用全部1
2
3firstletter :: String->String
firstletter ""="Empty String,whoops"
firstletter all@(x:_) = "The first letter of " ++ all ++ " is " ++ x
执行结果:1
2ghci>firstletter "Hello"
"The first letter of Hello is H"
检查参数是否为真1
2
3
4
5
6
7
8bmiTell::Double->Double->String
bmiTell weight height
| bmi <= 18.5 = "You're underweight,you emo,you"
| bmi <= 25.5 = "Normal"
| bmi <= 30.0 = "Fat"
| otherwise = "Whale"
where bmi = weight/height^2
运行结果:1
2ghci>bmiTell 70 1.69
"Normal"
where中模式匹配1
2
3
4initials :: String -> String -> String
initials firstname lastname = [f] ++ "." ++ [l]
where (f:_) = firstname
(l:_) = lastname
运行结果:1
2ghci>initials "xiaoyu" "cheng"
"x.c"
where中使用函数1
2
3calBmi::(Double,Double)->Double
calBmi (w,h) = bmi w h
where bmi weight height = weight/height^2
运行结果:1
2ghci>calBmi (70,1.69)
24.508945765204302
letlet
表达式的格式为let <bindings> in <expressions>
。let
中绑定的名字仅对in
部分可见。
- 在局部作用域中定义函数
1
2ghci>[let square x = x*x in (square 2,square 3,square 4)]
[(4,9,16)] - 一行中绑定多个名字
1
2ghci>(let a=100;b=200;c=300 in a*b*c,let foo="Hey";bar="there" in foo ++ bar)
(6000000,"Heythere") - 从元组中匹配取值
1
2ghci>(let (a,b,c) =(1,2,3) in a+b+c)*100
600 - 列表推导中使用
let
执行结果:1
2calcBmis :: [(Double,Double)] -> [Double]
calcBmis xs = [bmi|(w,h)<-xs,let bmi=w/h^2,bmi>25]1
2ghci>calcBmis
case表达式
case表达式语法结构:
case expression of pattern -> result
pattern -> result
pattern -> result
1 | describeList :: [a] -> String |
执行结果:1
2ghci>describeList []
"The list is empty."
递归
简单递归1
2
3
4maximum' ::(Ord a)=> [a]->a
maximum' []= error "maximum of empty list"
maximum' [x]=x
maximum' (x:xs)=max x (maximum' xs)
执行结果:1
2ghci>maximum [54,2,4,5,6,756,34]
756
replicate1
2
3
4replicate' :: Int -> a -> [a]
replicate' n x
| n <= 0 = []
| otherwise = x:replicate' (n-1) x
执行结果:1
2
3
4ghci>replicate' 10 'x'
"xxxxxxxxxx"
ghci>replicate' 10 8
[8,8,8,8,8,8,8,8,8,8]
take1
2
3
4
5take' :: (Num i,Ord i) => i ->[a]->[a]
take' n _
|n<=0 = []
take' _ [] = []
take' n (x:xs)= x:take' (n-1) xs
运行结果:1
2ghci>take' 2
reverse1
2
3reverse' :: [a]->[a]
reverse' []=[]
reverse' (x:xs) = reverse' xs ++ [x]
运行结果:1
2ghci>reverse' [1,2,3,4,5]
[5,4,3,2,1]
repeat1
2repeat' :: a->[a]
repeat' a = a:repeat' a
执行结果:1
2repeat' :: a->[a]
repeat' a = a:repeat' a
zip1
2
3
4zip' :: [a]->[b]->[(a,b)]
zip' _ [] = []
zip' [] _ = []
zip' (x:xs) (y:ys) = (x,y):zip' xs ys
执行结果:1
2ghci>zip [1,2] ['a','b']
[(1,'a'),(2,'b')]
elem1
2
3
4
5elem' :: (Eq a)=>a->[a]->Bool
elem' a [] = False
elem' a (x:xs)
| a==x = True
| otherwise = elem' a xs
执行结果:1
2ghci>elem' 1
True
快速排序1
2
3
4
5
6quicksort :: (Ord a) => [a]->[a]
quicksort [] = []
quicksort (x:xs) =
let smaller = [a|a<-xs,a<=x]
larger = [a|a<-xs,a>x]
in quicksort smaller ++ [x] ++ quicksort larger
执行结果:1
2ghci>quicksort [2,3,4,5,8,67,4,3,6,5,7,6,7,7]
[2,3,3,4,4,5,5,6,6,7,7,7,8,67]