从零开始
1> 5 / 222.53> True && False4False5> False || True6True7> not (True && True)8False9> 5 == 510True11> 5 /= 512False13> "Hello" == "Hello"14True15> 5 == "Hello" ;; Error 不同类型16> 5 + 4.0179.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" !! 16'a'7> [1, 2, 3] !! 081
:
用来添加到 List 前端,!!
用来通过下标取数,[1, 2, 3]
是 1:2:3:[]
的语法糖
1> [[1, 2], []]2[[1, 2], []]
List 中的 List 可以是不同长度的,但内部元素必须同类型
1> [3, 2, 1] > [2, 3, 4]2True3> [3, 4, 2] > [3, 4]4True5> [2, 3, 4] == [2, 3, 4]
List 的比较根据下标依次比较
List 中的各种操作:
1> head [1, 2, 3, 4]213> tail [1, 2, 3, 4]4[2, 3, 4]5> last [1, 2, 3, 4]647> init [1, 2, 3, 4]8[1, 2, 3]9> head [] ;; Error head、tail、last、init 都要小心空 List10> length [1, 2, 3]11312> null [1, 2, 3]13False14> null []15True16> 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]23324> minimum [1, 2, 3]25126> sum [1, 2, 3]27628> product [1, 2, 3, 4]292430> 4 `elem` [1, 2, 3, 4]31True32> 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] ;; 要避免小数 Range5> 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 1011[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 也可以储存 List3> [(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 zip2zip :: [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> rightTriangles3[(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 Lengthnull
:判断 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