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

来源:互联网 发布:若水软件 免费软件 编辑:程序博客网 时间:2024/06/06 09:28

Haskell 重要数据结构 - List

Table of Contents

  • 1 Haskell 基本知识
  • 2 Haskell 重要数据结构 - List
    • 2.1 List基本知识
    • 2.2 List 相关函数
    • 2.3 利用List特性编写程序

1 Haskell 基本知识

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] !! 21ghci>[4,2,1] !! 12ghci>[4,2,1] !! 04ghci>"hi,jack" !! 2','
  • List 的比较

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

ghci>[1,2] > [2,3]Falseghci>[1,2] > [1,1]Trueghci>[1,2,3] > [1,2]Trueghci>[1,2,-1] > [1,2]Trueghci>[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]Falseghci>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]6ghci>sum [1..10]55
  • product
ghci>product [4,2,5]40
  • elem
ghci>3 `elem` [6,7,8]Falseghci>6 `elem` [6,7,8]Trueghci>7 `elem` [6,7,8]Trueghci>elem 3 [6,7,8]Falseghci>elem 6 [6,7,8]Trueghci>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 33,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 headghci>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 xtell (x:y:[]) = "This list have two elemtn: " ++ show x ++ " and " ++ show ytell (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] = xmaxElem (x:xs) = max x (maxElem xs)
ghci>maxElem []*** Exception: Empty List have no max elementghci>maxElem [1]3ghci>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 [] = Falseelem' x (y:xs)  | x == y = True  | otherwise = elem' x xs
ghci>elem' 5 [2,5,7]Trueghci>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 [][]

原创粉丝点击