haskell中list and tuple 的基础函数总结

参考资料 :learnyouahaskell.com

List:

++ :连接两个list (Haskell里list和string基本相同)

  1. ghci> [1,2,3,4] ++ [9,10,11,12]  
  2. [1,2,3,4,9,10,11,12]  
  3. ghci> "hello" ++ " " ++ "world"  
  4. "hello world"  
  5. ghci> ['w','o'] ++ ['o','t']  
  6. "woot"  


: :takes a number and a list of numbers or a character and a list of characters, whereas ++ takes two lists. 连接一个数字或字符与List

  1. ghci> 'A':" SMALL CAT"  
  2. "A SMALL CAT"  
  3. ghci> 5:[1,2,3,4,5]  
  4. [5,1,2,3,4,5]  


!! :List切片

  1. ghci> "Steve Buscemi" !! 6  
  2. 'B'  
  3. ghci> [9.4,33.2,96.2,11.2,23.25] !! 1  
  4. 33.2  


<, >, >=, <= : list大小比较

  1. ghci> [3,2,1] > [2,1,0]  
  2. True  
  3. ghci> [3,2,1] > [2,10,100]  
  4. True  
  5. ghci> [3,4,2] > [3,4]  
  6. True  
  7. ghci> [3,4,2] > [2,4]  
  8. True  
  9. ghci> [3,4,2] == [3,4,2]  
  10. True  


head takes a list and returns its head. The head of a list is basically its first element.(提取第一个元素)

  1. ghci> head [5,4,3,2,1]  
  2. 5   

tail takes a list and returns its tail. In other words, it chops off a list's head.(抛去第一个元素)

  1. ghci> tail [5,4,3,2,1]  
  2. [4,3,2,1]   

last takes a list and returns its last element.(提取最后一个元素)

  1. ghci> last [5,4,3,2,1]  
  2. 1   

init takes a list and returns everything except its last element.(刨去最后一个元素)

  1. ghci> init [5,4,3,2,1]  
  2. [5,4,3,2]   

haskell中list and tuple 的基础函数总结(特别形象)

length takes a list and returns its length, obviously.(Python的len() )

  1. ghci> length [5,4,3,2,1]  
  2. 5  


null checks if a list is empty. If it is, it returns True, otherwise it returns False. Use this function instead of xs == [] (if you have a list called xs)

  1. ghci> null [1,2,3]  
  2. False  
  3. ghci> null []  
  4. True  

reverse reverses a list.

  1. ghci> reverse [5,4,3,2,1]  
  2. [1,2,3,4,5]  

take takes number and a list. It extracts that many elements from the beginning of the list. Watch.

  1. ghci> take 3 [5,4,3,2,1]  
  2. [5,4,3]  
  3. ghci> take 1 [3,9,3]  
  4. [3]  
  5. ghci> take 5 [1,2]  
  6. [1,2]  
  7. ghci> take 0 [6,6,6]  
  8. []  

See how if we try to take more elements than there are in the list, it just returns the list. If we try to take 0 elements, we get an empty list.

drop works in a similar way, only it drops the number of elements from the beginning of a list.

  1. ghci> drop 3 [8,4,2,1,5,6]  
  2. [1,5,6]  
  3. ghci> drop 0 [1,2,3,4]  
  4. [1,2,3,4]  
  5. ghci> drop 100 [1,2,3,4]  
  6. []   

maximum takes a list of stuff that can be put in some kind of order and returns the biggest element.

minimum returns the smallest.

  1. ghci> minimum [8,4,2,1,5,6]  
  2. 1  
  3. ghci> maximum [1,9,2,3,4]  
  4. 9   

sum takes a list of numbers and returns their sum.

product takes a list of numbers and returns their product.

  1. ghci> sum [5,2,1,6,3,2,5,7]  
  2. 31  
  3. ghci> product [6,2,1,2]  
  4. 24  
  5. ghci> product [1,2,5,6,7,9,2,0]  
  6. 0   

elem takes a thing and a list of things and tells us if that thing is an element of the list. It's usually called as an infix function because it's easier to read that way.

  1. ghci> 4 `elem` [3,4,5,6]  
  2. True  
  3. ghci> 10 `elem` [3,4,5,6]  
  4. False  

Those were a few basic functions that operate on lists. We'll take a look at more list functions later

关于tuple的基本函数

fst takes a pair and returns its first component.

  1. ghci> fst (8,11)  
  2. 8  
  3. ghci> fst ("Wow"False)  
  4. "Wow"  

snd takes a pair and returns its second component. Surprise!

  1. ghci> snd (8,11)  
  2. 11  
  3. ghci> snd ("Wow"False)  
  4. False  
Note: these functions operate only on pairs. They won't work on triples, 4-tuples, 5-tuples, etc. We'll go over extracting data from tuples in different ways a bit later.

A cool function that produces a list of pairs: zip. It takes two lists and then zips them together into one list by joining the matching elements into pairs. It's a really simple function but it has loads of uses. It's especially useful for when you want to combine two lists in a way or traverse two lists simultaneously. Here's a demonstration.

  1. ghci> zip [1,2,3,4,5] [5,5,5,5,5]  
  2. [(1,5),(2,5),(3,5),(4,5),(5,5)]  
  3. ghci> zip [1 .. 5] ["one""two""three""four""five"]  
  4. [(1,"one"),(2,"two"),(3,"three"),(4,"four"),(5,"five")]  

It pairs up the elements and produces a new list. The first element goes with the first, the second with the second, etc. Notice that because pairs can have different types in them, zip can take two lists that contain different types and zip them up. What happens if the lengths of the lists don't match?

  1. ghci> zip [5,3,2,6,2,7,2,5,4,6,6] ["im","a","turtle"]  
  2. [(5,"im"),(3,"a"),(2,"turtle")]  

The longer list simply gets cut off to match the length of the shorter one. Because Haskell is lazy, we can zip finite lists with infinite lists:

  1. ghci> zip [1..] ["apple""orange""cherry""mango"]  
  2. [(1,"apple"),(2,"orange"),(3,"cherry"),(4,"mango")]