重新学习 Haskell(第一部分:基础知识)
由 Mux 主办的 DEV 全球展示挑战赛:展示你的项目!
大约六个月前,我学完了《Learn You a Haskell for Great Good!》,之后就很少用 Haskell 了。所以,当然,我把所有东西都忘光了。因此,我又重新学习了一遍,并制作了这份方便的复习指南(主要是为了我自己,但也方便你快速回顾 Haskell 的工作原理):
not
Haskell 不使用~or!来表示否定,而是使用not函数:
ghci> not True -- comments come after two hyphens
False
但它的确过去/=常表示“不等于”:
ghci> 5 /= 4
True
功能应用
Haskell 不使用括号来分隔函数名和参数;而是直接使用空格:
ghci> max 3 4
4
部分功能应用程序
部分应用的功能可以保存到变量中,以便以后使用!
ghci> min5 = min 5
ghci> min5 4
4
ghci> min5 6
5
中缀表示法
有些函数放在两个参数之间比放在参数之前更有意义。例如:
ghci> div 100 5 -- prefix notation
20
ghci> 100 `div` 5 -- infix notation
20
函数
定义基本函数时,请在函数名之前添加命名参数=,并在函数名之后添加函数定义:
ghci> pythagoras a b = sqrt (a*a + b*b)
ghci> pythagoras 5 12
13.0
请注意,函数名称可以包含'(通常用于表示其他惰性函数的严格版本),并且不能以大写字母开头。
列表
在 Haskell 中,列表是同质的,而字符串是字符列表:
ghci> ll = [1,2,3,4]
ghci> ss = ['e','l','l','o']
可以使用 `prepend`(快速)将单个元素添加到列表的前面:,可以使用 `append`(慢速)将列表添加到列表的后面++:
ghci> 'h':ss
"hello"
ghci> ll ++ [5] ++ [6,7]
[1,2,3,4,5,6,7]
ghci> 's':'m':ss -- : and ++ can be chained like this
"smello"
可以使用运算符提取元素!!(与 C 语言中的运算符不同[]):
ghci> ll !! 2 -- equivalent to ll[2] in C
3
列表本身可以包含项目列表或项目列表的列表,所有这些列表的长度都可以不同,只要它们都包含相同类型的对象(数字或字符,但不能同时包含两者)。
最后,列表是按字典顺序进行比较的,也就是说,先比较第一个元素,再比较第二个元素,依此类推:
ghci> [1, 2, 3] > [0, 4, 5]
True
ghci> [1, 2, 3] > [1, 2]
True
列表函数
head/ tail/ init/ last/length
ghci> ll
[1,2,3,4]
ghci> head ll -- first element of list
1
ghci> tail ll -- all but first element of list
[2,3,4]
ghci> init ll -- all but last element of list
[1,2,3]
ghci> last ll -- last element of list
4
ghci> length ll -- length of list
4
注意head,` __init__` tail、 `__init__`init和last`__init__` 如果对空列表调用都会抛出错误,但 ` length__init__` 返回 0。您可以使用以下方法检查列表是否为空null:
ghci> length [] -- length of an empty list is zero
0
ghci> length ll -- ll has 4 elements in it at indices 0..3
4
ghci> null [] -- an empty list is null
True
ghci> null ll
False
reverse
ghci> reverse ll -- reverse a list
[4,3,2,1]
take/drop
ghci> take 1 ll -- take 1 element from the beginning of the list
[1]
ghci> take 3 ll -- take 3 elements
[1,2,3]
ghci> drop 1 ll -- drop 1 element from the beginning of the list
[2,3,4]
ghci> drop 3 ll -- drop 3 elements
[4]
maximum/minimum
ghci> maximum ll
4
ghci> minimum ll
1
sum/product
ghci> sum ll -- 1 + 2 + 3 + 4
10
ghci> product ll -- 1 * 2 * 3 * 4
24
elem
elem它的工作原理有点像contains()其他语言中的那种,如果第一个参数是第二个参数的元素(第二个参数必须是一个列表),则返回 true:
ghci> elem 2 ll
True
ghci> 5 `elem` ll
False
cycle//repeatreplicate
使用 `\n` 重复列表cycle;使用 `\n` 重复单个对象repeat
ghci> take 10 (cycle ll)
[1,2,3,4,1,2,3,4,1,2]
ghci> take 10 (repeat 5)
[5,5,5,5,5,5,5,5,5,5]
ghci> take 5 (repeat ll)
[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
ghci> replicate 10 5 -- same as take 10 (repeat 5)
[5,5,5,5,5,5,5,5,5,5]
范围
范围可以处理整数和字符:
ghci> [1..10]
[1,2,3,4,5,6,7,8,9,10]
ghci> ['a'..'j']
"abcdefghij"
列表的前两个元素可以用来定义一个模式:
ghci> ['z','w'..'a']
"zwtqnkheb"
ghci> [26,23..1]
[26,23,20,17,14,11,8,5,2]
上面的列表会在到达最后一个元素时结束,但你也可以使用以下方式定义所需的元素数量take:
ghci> take 10 [1,3..]
[1,3,5,7,9,11,13,15,17,19]
第二部分将介绍:列表推导式、元组和类型类
文章来源:https://dev.to/awwsmm/relearn-you-a-haskell-part-1-the-basics-i9l