Haskell 学习笔记(一)
Haskell 基础语法
基础语法
个人学习Haskell 学习笔记
ghci
安装完毕 在termial中输入 ghc --version 查看当前版本
The Glorious Glasgow Haskell Compilation System, version 8.6.3
输入ghci 进入
10-248-92-59:cs381 nmc$ ghci
GHCi, version 8.6.3: http://www.haskell.org/ghc/ :? for help
Prelude>
简单基础运算
Prelude> 2 + 10
12
Prelude> 3 * 6
18
Prelude> 2 / 5
0.4
Prelude> 2 * 3 + 10
16
Prelude> ( 2 * 3 ) + 10
16
Prelude> 2 * (3 + 10 )
26
Prelude> True && False
False
Prelude> True || False
True
Prelude> not True
False
Prelude> 1 == 1
True
Prelude> "hello" == "hello"
True
ctrl+D 退出
简单函数指令
- succ
- max
- min
- div
- foo
- bar
定义自己函数
vim demo1.hs
打开编辑器 输入 自己的函数
addOne x = x + 1
double x = x + x
运行 ghci 输入**:l demo1**
GHCi, version 8.6.3: http://www.haskell.org/ghc/ :? for help
Prelude> :l demo1
[1 of 1] Compiling Main ( demo1.hs, interpreted )
Ok, one module loaded.
*Main> double 2
4
*Main> addOne 2
3
或者 ghc --make demo1 编译后 ./demo1 执行
list
1. let
可以直接赋值
Prelude> let a = 1
Prelude> a
1
Prelude> let aset = [1,2,3,4]
Prelude> aset
[1,2,3,4]
“hello” 存在list 里面是 [‘h’,‘e’,‘l’,‘l’,‘o’]
2. ++
Prelude> [1,2,3,4] ++ [1,2]
[1,2,3,4,1,2]
Prelude> "hello" ++ " " ++ "world"
"hello world"
Prelude> ['h','e','l'] ++ ['l','o']
"hello"
3. :
在list开头放置元素
Prelude> 'H':"ello"
"Hello"
Prelude> 5:[1,2,3,4]
[5,1,2,3,4]
我们可以得知[1,2,3] 实际上是 1:2:3:[ ]
[ ] 是一个空的list
[ ] , [ [ ] ] , [ [ ] [ ] [ ] ] 是三个不同的东西
4. !!
通过索引得到元素 从0 开始
Prelude> "hello" !! 2
'l'
Prelude> "hello" !! 7
*** Exception: Prelude.!!: index too large
Prelude> [1,2,3,4,5] !! 2
3
5. > , >= , < , <=
按字典顺序,先对比首元素依次比较
Prelude> [2,1] > [2,2]
False
Prelude> [2,1] > [1,2]
True
Prelude> [2,1] > [1]
True
Prelude> [2,1] > [1,3,3]
True
6. head, tail 和 last , init
Prelude> head [1,2,3,4]
1
Prelude> tail [1,2,3,4]
[2,3,4]
Prelude> last [1,2,3,4]
4
Prelude> init [1,2,3,4]
[1,2,3]
7. length null reverse
求长度 ,检验是否为空 和 反转
Prelude> length [1,2,3,4]
4
Prelude> null [1,2,3,4]
False
Prelude> null []
True
Prelude> reverse [1,2,3,4]
[4,3,2,1]
8. take 和 drop
提取元素从开头按个数 得到的是list 而不是单个元素
Prelude> take 1 [1,2,3,4]
[1]
Prelude> take 4 [1,2,3,4]
[1,2,3,4]
Prelude> take 10 [1,2,3,4]
[1,2,3,4]
Prelude> take 0 [1,2,3,4]
[]
drop 与take 同理
Prelude> drop 1 [1,2,3,4]
[2,3,4]
Prelude> drop 4 [1,2,3,4]
[]
Prelude> drop 10 [1,2,3,4]
[]
9. sum
sum “ 嘘 嘘 嘘 求和!”
Prelude> sum [1,2,3,4]
10
Prelude> sum 1 2
<interactive>:42:1: error:
• Non type-variable argument in the constraint: Num (t2 -> t3)
(Use FlexibleContexts to permit this)
• When checking the inferred type
it :: forall (t1 :: * -> *) t2 t3.
(Foldable t1, Num t2, Num (t1 (t2 -> t3)), Num (t2 -> t3)) =>
t3
10. maximum minimum
max 与maximum 不同
max 是对比两个数中的大小 而maximum对比的是list中
Prelude> maximum [1,2,3,4]
4
Prelude> max 1 2
2
Prelude> max [1,2,3,4]
<interactive>:38:1: error:
• No instance for (Show ([Integer] -> [Integer]))
arising from a use of ‘print’
(maybe you haven't applied a function to enough arguments?)
• In a stmt of an interactive GHCi command: print it
Prelude> max 1 2 3
<interactive>:39:1: error:
• Non type-variable argument in the constraint: Ord (t1 -> t2)
(Use FlexibleContexts to permit this)
• When checking the inferred type
it :: forall t1 t2. (Ord (t1 -> t2), Num t1, Num (t1 -> t2)) => t2
min 和 minimum 同理
11. product
Prelude> product [2,3]
6
Prelude> product [1,2,3]
6
Prelude> product 1 2
<interactive>:45:1: error:
• Non type-variable argument in the constraint: Num (t2 -> t3)
(Use FlexibleContexts to permit this)
• When checking the inferred type
it :: forall (t1 :: * -> *) t2 t3.
(Foldable t1, Num t2, Num (t1 (t2 -> t3)), Num (t2 -> t3)) =>
t3
12. elem
list 中是否存在某元素 bool
Prelude> elem 1 [1,2,3,4]
True
Prelude> elem 5 [1,2,3,4]
False
德州范围(枚举)
1. ". ."来实现枚举
使用 . .
Prelude> [1 .. 10]
[1,2,3,4,5,6,7,8,9,10]
Prelude> ['a' .. 'f']
"abcdef"
Prelude> ['A' .. 'F']
"ABCDEF"
2. 规律枚举
同时可以进行规律性的枚举
Prelude> [1,3..19]
[1,3,5,7,9,11,13,15,17,19]
Prelude> [2,4..20]
[2,4,6,8,10,12,14,16,18,20]
Prelude> [3,6..33]
[3,6,9,12,15,18,21,24,27,30,33]
Prelude> [1,3,5..19]
<interactive>:61:7: error: parse error on input ‘..’
3.无限循环数列
cycle 和 repeat
可以实现无限循环的数列 一般用take 取所需的list
Prelude> take 9 (cycle [1,2,3])
[1,2,3,1,2,3,1,2,3]
Prelude> take 12 (cycle "WTF")
"WTFWTFWTFWTF"
Prelude> take 6 (repeat 6)
[6,6,6,6,6,6]
4. 取范围数值
取范围内数据
注意mod不是 ‘mod’
Prelude> [x*2 | x <- [1..10] ]
[2,4,6,8,10,12,14,16,18,20]
Prelude> [x*2 | x <- [1..10], x*2 >=12]
[12,14,16,18,20]
Prelude> [ x | x <-[50..100], x 'mod' 7 == 3]
<interactive>:69:24: error:
• Syntax error on 'mod'
Perhaps you intended to use TemplateHaskell or TemplateHaskellQuotes
• In the Template Haskell quotation 'mod'
Prelude> [ x | x <-[50..100], x `mod` 7 == 3]
[52,59,66,73,80,87,94]
Prelude> [ x | x <- [10..20], x /= 13, x /= 15, x /= 19]
[10,11,12,14,16,17,18,20]
Prelude> [ x*y | x <- [2,5,10], y <- [8,10,11]]
[16,20,22,40,50,55,80,100,110]
5. 例子:非系统的length
length’ xs = sum [1 | _ <- xs]
'代表非标准函数
枚举xs中所有的替换成1 然后求和 就得到了长度
元组 Tuples
1. 元组定义
在某些方面,元组就像列表 - 它们是将多个值存储到单个值中的一种方法。 但是,存在一些根本差异。 数字列表是一个数字列表。 这是它的类型,如果它只有一个数字或无限数量也没关系。 但是,当您确切知道要组合的值的数量时,将使用元组,其类型取决于它具有的组件数量和组件类型。 它们用括号表示,它们的组件用逗号分隔。
[(1,2),(8,11),(4,5)]
(1,2)双元组
(1,2,3)三元组
2. fst 和 snd
取二元组第一个元素 和第二个元素
Prelude> fst (1,2)
1
Prelude> fst ("lol", False)
"lol"
Prelude> snd (1,2)
2
Prelude> snd ("lol", False)
False
3. ZIP
用于讲两个匹配元素链接 压缩成为一个list
Prelude> zip [1,2,3,4,5] [5,5,5,5,5]
[(1,5),(2,5),(3,5),(4,5),(5,5)]
Prelude> zip [1,2,3,4,5] ["A","B","C"]
[(1,"A"),(2,"B"),(3,"C")]
Prelude> zip [1..] ["A","B","C","D"]
[(1,"A"),(2,"B"),(3,"C"),(4,"D")]
4. 直角三角形例子
用一个三元组 存abc三条边
let triangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a <- [1..10] ]
Prelude> triangles
[(1,1,1),(2,1,1),(3,1,1),(4,1,1),(5,1,1),(6,1,1),(7,1,1),(8,1,1),(9,1,1), ........................(1,10,10),(2,10,10),(3,10,10),(4,10,10),(5,10,10),(6,10,10),(7,10,10),(8,10,10),(9,10,10),(10,10,10)]
加上限制条件求直角三角形
Prelude> let rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2]
Prelude> rightTriangles
[(3,4,5),(6,8,10)]
加上abc边的要求
Prelude> let rightTriangles' = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2, a+b+c == 24]
Prelude> rightTriangles'
[(6,8,10)]
Reference page
[1]: http://learnyouahaskell.com/starting-out#ready-set-go
上一篇: Python 函数的模块化
下一篇: Acwing 语法基础课上课笔记