「学习总结-Haskell-2」Haskell重要数据结构:List

Haskell 重要数据结构 - List

2 Haskell 重要数据结构 - List

2.1 List基本知识

  • List 表示

例1 基本元素为整数的List

ghci>let tList = [1,2,3,4]
ghci>tList 
[1,2,3,4]

例2 基本元素为List的List

ghci>[[1,2],[3],[4,5,6]]
[[1,2],[3],[4,5,6]]

例3 基本元素为字符的List,即字符串

ghci>['h','e','l','l','o']
"hello"

例4 List列举表示

ghci>[1..10]
[1,2,3,4,5,6,7,8,9,10]
ghci>['a'..'e']
"abcde"
ghci>[2,4..10]
[2,4,6,8,10]
ghci>[5..2]
[]
ghci>[5,4..2]
[5,4,3,2]

例5 List的描述表示

ghci>[x | x <- [1..10], x > 3, x < 7]
[4,5,6]
ghci>[x + y | x <- [1,2,3], y <- [4,5]]
[5,6,6,7,7,8]

例6 List是什么 List就是相同类型的元素粘在一起

ghci>3:[4,5]
[3,4,5]
ghci>3:4:[5]
[3,4,5]
ghci>3:4:5:[]
[3,4,5]
  • List基本操作
    • List 连接
ghci>[1,2,3] ++ [4,5]
[1,2,3,4,5]
  • List 元素的访问
ghci>[4,2,1] !! 2
1
ghci>[4,2,1] !! 1
2
ghci>[4,2,1] !! 0
4
ghci>"hi,jack" !! 2
','
  • List 的比较

List的比较方式是按字典顺序

ghci>[1,2] > [2,3]
False
ghci>[1,2] > [1,1]
True
ghci>[1,2,3] > [1,2]
True
ghci>[1,2,-1] > [1,2]
True
ghci>[1,2] > [1,2,3]
False

2.2 List 相关函数

  • head
ghci>head [3,1,4]
3
  • tail
ghci>tail [3,1,4]
[1,4]
  • init
ghci>init [3,1,4]
[3,1]
  • last
ghci>last [3,1,4]
4
  • length
ghci>length [3,1,4]
3
  • null
ghci>null [3,1,4]
False
ghci>null []
True
  • reverse
ghci>reverse [1,2,3,4]
[4,3,2,1]
  • take
ghci>take 2 [3,1,5]
[3,1]
ghci>take 1 [3,1,5]
[3]
ghci>take 0 [3,1,5]
[]
  • drop
ghci>drop 2 [3,1,5]
[5]
ghci>drop 1 [3,1,5]
[1,5]
ghci>drop 0 [3,1,5]
[3,1,5]
  • maximum
ghci>maximum [1,5,2,3]
5
  • sum
ghci>sum [1,2,3]
6
ghci>sum [1..10]
55
  • product
ghci>product [4,2,5]
40
  • elem
ghci>3 `elem` [6,7,8]
False
ghci>6 `elem` [6,7,8]
True
ghci>7 `elem` [6,7,8]
True
ghci>elem 3 [6,7,8]
False
ghci>elem 6 [6,7,8]
True
ghci>elem 7 [6,7,8]
True
  • cycle
ghci>cycle [5,2,8]
[5,2,8,5,2,8,5,2,8,5,2,8,...] --无穷重复下去
ghci>take 5 (cycle [5,2,8])
[5,2,8,5,2]
ghci>take 10 (cycle [5,2,8])
[5,2,8,5,2,8,5,2,8,5]
  • repeat
ghci>repeat 3
3,3,3,...
ghci>take 5 (repeat 3)
[3,3,3,3,3]
  • replicate
ghci>replicate 2 10
[10,10]
ghci>replicate 3 10
[10,10,10]

2.3 利用List特性编写程序

  • 利用List描述表示方法

例1

boomBang xs = [ if x < 3 then "BOOM" else "BANG" | x <- xs ]
ghci>:l listProg.hs
[1 of 1] Compiling Main             ( listProg.hs, interpreted )
Ok, modules loaded: Main.
ghci>boomBang [1..5]
["BOOM","BOOM","BANG","BANG","BANG"]
ghci>boomBang [0..4]
["BOOM","BOOM","BOOM","BANG","BANG"]

例2

cartesianproduc xs ys = [ [x,y] | x <- xs, y <- ys]
ghci>cartesianproduc [3,2] [1,5,6]
[[3,1],[3,5],[3,6],[2,1],[2,5],[2,6]]

例3

removeNonUppercase xs = [ x | x <- xs, x `elem` ['A'..'Z'] ]
ghci>removeUppercase "SDFAasdfRQf"
"SDFARQ"
  • List 与 模式匹配(Pattern Matching)

例1

head' [] = error "empty list can not be head"
head' (x:_) = x
ghci>head' []
*** Exception: empty list can not be head
ghci>head' "dafs"
'd'
ghci>head' [3,4,1]
3

例2

tell [] = "This is a empty list"
tell (x:[]) = "This list have only one element:" ++ show x
tell (x:y:[]) = "This list have two elemtn: " ++ show x ++ " and " ++ show y
tell (x:y:_) = "This is a long list"
ghci>tell []
"This is a empty list"
ghci>tell [3]
"This list have only one element:3"
ghci>tell [3,4]
"This list have two elemtn: 3 and 4"
ghci>tell [3,4,5,6]
"This is a long list"
ghci>tell "sdfa"
"This is a long list"
ghci>tell "sd"
"This list have two elemtn: 's' and 'd'"

例3

abbrev firstName lastName = [f] ++ "." ++ [l]
  where (f:_) = firstName
        (l:_) = lastName
ghci>abbrev "Tom" "Hacks"
"T.H"
ghci>abbrev "Alan" "Kay"
"A.K"

例4

describeList ls = "This list is "
                  ++ case ls of []  -> "empty"
                                [x] -> "a singleton list"
                                xs -> "a long list"
ghci>describeList []
"This list is empty"
ghci>describeList "a"
"This list is a singleton list"
ghci>describeList "hello"
"This list is a long list"
  • List 与 递归 (Recursion)

例1

maxElem [] = error "Empty List have no max element"
maxElem [x] = x
maxElem (x:xs) = max x (maxElem xs)
ghci>maxElem []
*** Exception: Empty List have no max element
ghci>maxElem [1]
3
ghci>maxElem [3,2,6,1]
6

例2

replicate' n x
  | n <= 0 = []
  | otherwise = x : replicate' (n-1) x
ghci>replicate' 0 2
[]
ghci>replicate' 5 2
[2,2,2,2,2]

例3

take' n _
  | n <= 0 = []
take' _ [] = []
take' n (x:xs) = x : take' (n-1) xs
ghci>take' (-3) [1,2]
[]
ghci>take' 4 [1..10]
[1,2,3,4]

例4

reverse' [] = []
reverse' (x:xs) = reverse' xs ++ [x]
ghci>reverse' []
[]
ghci>reverse' [1..10]
[10,9,8,7,6,5,4,3,2,1]

例5

elem' x [] = False
elem' x (y:xs)
  | x == y = True
  | otherwise = elem' x xs
ghci>elem' 5 [2,5,7]
True
ghci>elem' 5 [2,7]
False

例6 quicksort

quickSort [] = []
quickSort (x:xs) =
  let smallerPart = [a | a <- xs, a <= x]
      largerPart = [a | a <- xs, a > x]
  in quickSort smallerPart ++ [x] ++ quickSort largerPart

注意不能写成[a | a <= x, a <-xs]的形式,会提示a不在域内, 先写a <-xs , 才能决定 a <= x 是否成立。

ghci>quickSort [2,6,1,8,4]
[1,2,4,6,8]
ghci>quickSort []
[]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值