Skip to content

AHABHGK

从零开始

1> 5 / 2
22.5
3> True && False
4False
5> False || True
6True
7> not (True && True)
8False
9> 5 == 5
10True
11> 5 /= 5
12False
13> "Hello" == "Hello"
14True
15> 5 == "Hello" ;; Error 不同类型
16> 5 + 4.0
179.0 ;; 5 既可以看作整形也可以看作浮点数,4.0 只能看做浮点数

5 / 2 中的 / 称为中缀函数

1> succ 9 + max 5 4 + 1 ;; succ 返回一个数的后继
216 ;; 函数有最高的调用优先级,相当于 (succ 9) + (max 5 4) + 1

bar (bar 3) 不表示 bar 和 3 作为 bar 的两个参数,表示 bar 3 的结果作为 bar 的参数

1> doubleSmallNumber x = if x > 100 then x else x * 2

if 语句是个表达式,必须有返回结果

1> [1, 2, 3, 4] ++ [5, 6, 7, 8]
2[1, 2, 3, 4, 5, 6, 7, 8]
3> "Hello" ++ " " ++ "World"
4"Hello World"
5> ['H', 'e', 'l', 'l', 'o'] ++ [' '] ++ ['W', 'o', 'r', 'l', 'd']
6"Hello World"

List 内部类型必须相同,不关心数量,++ 用来连接两个 List,字符串是字符列表的语法糖

1> 'A' : " SMALL CAT"
2"A SMALL CAT"
3> 1 : [2, 3, 4]
4[1, 2, 3, 4]
5> "Cat" !! 1
6'a'
7> [1, 2, 3] !! 0
81

: 用来添加到 List 前端,!! 用来通过下标取数,[1, 2, 3]1:2:3:[] 的语法糖

1> [[1, 2], []]
2[[1, 2], []]

List 中的 List 可以是不同长度的,但内部元素必须同类型

1> [3, 2, 1] > [2, 3, 4]
2True
3> [3, 4, 2] > [3, 4]
4True
5> [2, 3, 4] == [2, 3, 4]

List 的比较根据下标依次比较

List 中的各种操作:

1> head [1, 2, 3, 4]
21
3> tail [1, 2, 3, 4]
4[2, 3, 4]
5> last [1, 2, 3, 4]
64
7> init [1, 2, 3, 4]
8[1, 2, 3]
9> head [] ;; Error headtaillastinit 都要小心空 List
10> length [1, 2, 3]
113
12> null [1, 2, 3]
13False
14> null []
15True
16> reverse [1, 2, 3]
17[3, 2, 1]
18> take 2 [1, 2, 3]
19[1, 2]
20> drop 2 [1, 2, 3]
21[3]
22> maximum [1, 2, 3]
233
24> minimum [1, 2, 3]
251
26> sum [1, 2, 3]
276
28> product [1, 2, 3, 4]
2924
30> 4 `elem` [1, 2, 3, 4]
31True
32> 0 `elem` [1, 2, 3, 4]
33False

Range

1> [2, 4 .. 10]
2[2, 4, 6, 8, 10]
3> [0.1, 0.3 .. 1]
4[0.1, 0.3, 0.5, 0.7, 0.8999999999999999, 1.0999999999999999] ;; 要避免小数 Range
5> take 24 [13, 26 ..] ;; Haskell 是惰性的,它不会对无限长度的 List 求值
6> take 10 (cycle [1, 2, 3])
7[1, 2, 3, 1, 2, 3, 1, 2, 3, 1]
8> take 5 (repeat 1)
9[1, 1, 1, 1, 1]
10> replicate 3 10
11[10, 10, 10]

List Comprehension

1> [ x * 2 | x <- [1..5] ]
2[2, 4, 6, 8, 10]
3> boomBangs xs = [ if x < 10 then "BOOM!" else "BANG!" | x <- xs, odd x]
4> boomBangs [7..13]
5["BOOM!","BOOM!","BANG!","BANG!"]
6> [ x * y | x <- [2, 5, 10], y <- [8, 10, 11] ]
7[16,20,22,40,50,55,80,100,110]
8> length xs = sum [ 1 | _ <- xs ]
9> evenList = [ [ x | x <- xs, even x ] | xs <- xxs ]
10> evenList [[1, 3, 5, 2, 4], [6, 8, 9, 5]]
11[[2, 4], [6, 8]]

Tuple 数量明确,不关心类型

1> [(1, "two"), (3, "four")]
2[(1, "two"), (3, "four")] ;; Tuple 也可以储存 List
3> [(1, 2), (3, 4, 5)] ;; Error 数量不同
4> [(1, "two"), (3, 4)]
1> fst ("Wow", False)
2"Wow"
3> snd ("Wow", True)
4True

fst snd 只对 Pair Tuple 有效

1> :t zip
2zip :: [a] -> [b] -> [(a, b)]
3> zip [1 .. 5] ["one", "two", "three", "four", "five"]
4[(1,"one"),(2,"two"),(3,"three"),(4,"four"),(5,"five")]
1> let rightTriangles = [ (a, b, c) | c <- [1..10], b <- [1..c], a <- [1..b], a ^ 2 + b ^ 2 == c ^ 2 ]
2> rightTriangles
3[(3,4,5),(6,8,10)]

Sum

  • not:非

  • /=:不等

  • ++: List 拼接

  • 'a':"bc":List 加入头部

  • [1, 2, 3] !! 0:取下标

  • head tail last init take drop:List 截取操作,小心空数组

  • length:取 List Length

  • null:判断 List 是否为空

  • reverse:List 逆序

  • minimum maximum:List 最大最小

  • sum product:List 求和求积

  • 1 `elem` [1, 2, 3]:判断 List 是否存在该元素

  • [2, 4..10]:Range

  • [ x * 2 | x <- [1..5] ]:List Comprehension

  • (True, 1):Tuple