一些零碎的代码,摘自 《 Haskell 趣学指南》,包含常用函数等。

succ

succ 返回后继

1
2
ghci> succ 8  
9

max

max 返回最大值

1
2
ghci> max 100 101  
101

min

min 返回最小值

1
2
3
4
ghci> min 9 10  
9
ghci> min 3.4 3.2
3.2

!!

!! 按索引获取元素

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

head 取列表的第一个元素

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

tail

tail 取列表除了第一个元素的所有元素

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

last

last 取列表的最后一个元素

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

init

init 取列表除了最后一个元素的所有元素

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

inits

inits 重复取 init

1
2
ghci> inits "w00t"  
["","w","w0","w00","w00t"]

tails

tails 重复取 tail

1
2
3
4
ghci> tails "w00t"  
["w00t","00t","0t","t",""]
ghci> let w = "w00t" in zip (inits w) (tails w)
[("","w00t"),("w","00t"),("w0","0t"),("w00","t"),("w00t","")]

length

length 获取列表的长度

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

null

null 判断列表是否为空

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

reverse

reverse 反转一个列表

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

take

take 拿取一定数量的元素

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

drop

drop 抛弃一定数量的元素

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

maximum

maximum 返回列表最大值

1
2
ghci> maximum [1,9,2,3,4]  
9

minimum

minimum 返回列表最小值

1
2
ghci> minimum [8,4,2,1,5,6]  
1

sum

sum 列表求和

1
2
ghci> sum [5,2,1,6,3,2,5,7]  
31

product

product 列表求积

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

elem

elem 判断元素是否存在

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

elemIndex

elemIndex 返回第一个指定元素索引

1
2
3
4
5
6
ghci> :t elemIndex  
elemIndex :: (Eq a) => a -> [a] -> Maybe Int
ghci> 4 `elemIndex` [1,2,3,4,5,6]
Just 3
ghci> 10 `elemIndex` [1,2,3,4,5,6]
Nothing

elemIndices

elemIndices 返回所有指定元素索引

1
2
ghci> ' ' `elemIndices` "Where are the spaces?"  
[5,9,13]

find

find 查找符合条件的元素

1
2
3
4
5
6
ghci> find (>4) [1,2,3,4,5,6]  
Just 5
ghci> find (>9) [1,2,3,4,5,6]
Nothing
ghci> :t find
find :: (a -> Bool) -> [a] -> Maybe a

findIndex

findIndex 返回符合条件的元素索引

1
2
3
4
5
6
ghci> findIndex (==4) [5,3,2,1,6,4]  
Just 5
ghci> findIndex (==7) [5,3,2,1,6,4]
Nothing
ghci> findIndices (`elem` ['A'..'Z']) "Where Are The Caps?"
[0,6,10,14]

cycle

cycle 循环拼接列表

1
2
3
4
ghci> take 10 (cycle [1,2,3])  
[1,2,3,1,2,3,1,2,3,1]
ghci> take 12 (cycle "LOL ")
"LOL LOL LOL "

repeat

repeat 重复指定元素

1
2
ghci> take 10 (repeat 5)  
[5,5,5,5,5,5,5,5,5,5]

zip

zip 压缩函数,类似拉链

1
2
3
4
5
6
7
8
ghci> zip [1,2,3,4,5] [5,5,5,5,5]  
[(1,5),(2,5),(3,5),(4,5),(5,5)]
ghci> zip [1 .. 5] ["one", "two", "three", "four", "five"]
[(1,"one"),(2,"two"),(3,"three"),(4,"four"),(5,"five")]
ghci> zip [5,3,2,6,2,7,2,5,4,6,6] ["im","a","turtle"]
[(5,"im"),(3,"a"),(2,"turtle")]
ghci> zip [1..] ["apple", "orange", "cherry", "mango"]
[(1,"apple"),(2,"orange"),(3,"cherry"),(4,"mango")]

zipWith

zipWith 用指定的方式 zip

1
2
3
4
5
6
7
8
9
10
ghci> zipWith (+) [4,2,5,6] [2,6,2,3]  
[6,8,7,9]
ghci> zipWith max [6,3,2,1] [7,3,1,5]
[7,3,2,5]
ghci> zipWith (++) ["foo ", "bar ", "baz "] ["fighters", "hoppers", "aldrin"]
["foo fighters","bar hoppers","baz aldrin"]
ghci> zipWith (*) (replicate 5 2) [1..]
[2,4,6,8,10]
ghci> zipWith (zipWith (*)) [[1,2,3],[3,5,6],[2,3,4]] [[3,2,2],[3,4,5],[5,4,3]]
[[3,4,6],[9,20,30],[10,12,12]]

zip3

zip3zip 的 3 个参数版本

1
2
ghci> zip3 [2,3,3] [2,2,2] [5,5,3] 
[(2,2,5),(3,2,5),(3,2,3)]

zipWith3

zipWith3zipWith 的 3 个参数版本

1
2
ghci> zipWith3 (\x y z -> x + y + z) [1,2,3] [4,5,2,2] [2,2,3]  
[7,9,8]

map

map 对列表每个元素应用函数

1
2
3
4
5
6
7
8
9
10
ghci> map (+3) [1,5,3,1,6]  
[4,8,6,4,9]
ghci> map (++ "!") ["BIFF", "BANG", "POW"]
["BIFF!","BANG!","POW!"]
ghci> map (replicate 3) [3..6]
[[3,3,3],[4,4,4],[5,5,5],[6,6,6]]
ghci> map (map (^2)) [[1,2],[3,4,5,6],[7,8]]
[[1,4],[9,16,25,36],[49,64]]
ghci> map fst [(1,2),(3,5),(6,3),(2,6),(2,5)]
[1,3,6,2,2]

filter

filter 过滤出列表中复合条件的元素

1
2
3
4
5
6
7
8
9
10
11
12
ghci> filter (>3) [1,5,3,2,1,6,4,3,2,1]  
[5,6,4]
ghci> filter (==3) [1,2,3,4,5]
[3]
ghci> filter even [1..10]
[2,4,6,8,10]
ghci> let notNull x = not (null x) in filter notNull [[1,2,3],[],[3,4,5],[2,2],[],[],[]]
[[1,2,3],[3,4,5],[2,2]]
ghci> filter (`elem` ['a'..'z']) "u LaUgH aT mE BeCaUsE I aM diFfeRent"
"uagameasadifeent"
ghci> filter (`elem` ['A'..'Z']) "i lauGh At You BecAuse u r aLL the Same"
"GAYBALLS"

takeWhile

takeWhile 一直 take 直到条件不满足

1
2
3
4
5
6
ghci> takeWhile (/=' ') "elephants know how to party"
"elephants"
ghci> sum (takeWhile (<10000) (filter odd (map (^2) [1..])))
166650
ghci> sum (takeWhile (<10000) [n^2 | n <- [1..], odd (n^2)])
166650

dropWhile

dropWhile 一直 drop 直到条件不满足

1
2
3
4
ghci> dropWhile (/=' ') "This is a sentence"  
" is a sentence"
ghci> dropWhile (<3) [1,2,2,2,3,4,5,4,3,2,1]
[3,4,5,4,3,2,1]

intersperse

intersperse 在列表元素之间插入指定元素

1
2
3
4
ghci> intersperse '.' "MONKEY"  
"M.O.N.K.E.Y"
ghci> intersperse 0 [1,2,3,4,5,6]
[1,0,2,0,3,0,4,0,5,0,6]

intercalate

intercalate 将一个列表插入到另一个列表元素之间

1
2
3
4
ghci> intercalate " " ["hey","there","guys"]  
"hey there guys"
ghci> intercalate [0,0,0] [[1,2,3],[4,5,6],[7,8,9]]
[1,2,3,0,0,0,4,5,6,0,0,0,7,8,9]

transpose

transpose 转置矩阵

1
2
3
4
ghci> transpose [[1,2,3],[4,5,6],[7,8,9]]  
[[1,4,7],[2,5,8],[3,6,9]]
ghci> transpose ["hey","there","guys"]
["htg","ehu","yey","rs","e"]

concat

concat 拍扁列表

1
2
3
4
ghci> concat ["foo","bar","car"]  
"foobarcar"
ghci> concat [[3,4,5],[2,3,4],[2,1,1]]
[3,4,5,2,3,4,2,1,1]

concatMap

concatMapmapconcat

1
2
ghci> concatMap (replicate 4) [1..3]  
[1,1,1,1,2,2,2,2,3,3,3,3]

and

and 返回列表是否全是 true

1
2
3
4
ghci> and $ map (>4) [5,6,7,8]  
True
ghci> and $ map (==4) [4,4,4,3,4]
False

or

or 返回列表是否存在 true

1
2
3
4
ghci> or $ map (==4) [2,3,4,5,6,1]  
True
ghci> or $ map (>4) [1,2,3]
False

any

any 返回列表是否存在元素满足条件

1
2
3
4
ghci> any (==4) [2,3,5,6,1,4]  
True
ghci> any (`elem` ['A'..'Z']) "HEYGUYSwhatsup"
True

all

all 返回列表是否全部元素满足条件

1
2
3
4
ghci> all (>4) [6,9,10]  
True
ghci> all (`elem` ['A'..'Z']) "HEYGUYSwhatsup"
False

iterate

iterate 返回重复迭代的结果

1
2
3
4
ghci> take 10 $ iterate (*2) 1  
[1,2,4,8,16,32,64,128,256,512]
ghci> take 3 $ iterate (++ "haha") "haha"
["haha","hahahaha","hahahahahaha"]

splitAt

splitAt 在指定位置分隔数组

1
2
3
4
5
6
7
8
ghci> splitAt 3 "heyman"  
("hey","man")
ghci> splitAt 100 "heyman"
("heyman","")
ghci> splitAt (-3) "heyman"
("","heyman")
ghci> let (a,b) = splitAt 3 "foobar" in b ++ a
"barfoo"

span

span 将列表分割为满足条件的列表和不满足条件的列表

1
2
ghci> span (/=4) [1,2,3,4,5,6,7]  
([1,2,3],[4,5,6,7])

break

break 在满足条件时分割列表

1
2
ghci> break (==4) [1,2,3,4,5,6,7]  
([1,2,3],[4,5,6,7])

partition

partition 类似 spanbreak 不过会对整个列表进行过滤。

1
2
3
4
5
6
ghci> partition (`elem` ['A'..'Z']) "BOBsidneyMORGANeddy"  
("BOBMORGAN","sidneyeddy")
ghci> partition (>3) [1,3,5,6,3,2,1,0,3,7]
([5,6,7],[1,3,3,2,1,0,3])
ghci> span (`elem` ['A'..'Z']) "BOBsidneyMORGANeddy"
("BOB","sidneyMORGANeddy")

sort

sort 列表排序

1
2
3
4
ghci> sort [8,5,3,2,1,6,4,2]  
[1,2,2,3,4,5,6,8]
ghci> sort "This will be sorted soon"
" Tbdeehiillnooorssstw"

group

group 列表分组

1
2
3
4
ghci> group [1,1,1,1,2,2,2,2,3,3,2,2,2,5,6,7]  
[[1,1,1,1],[2,2,2,2],[3,3],[2,2,2],[5],[6],[7]]
ghci> map (\l@(x:xs) -> (x,length l)) . group . sort $ [1,1,1,1,2,2,2,2,3,3,2,2,2,5,6,7]
[(1,4),(2,7),(3,2),(5,1),(6,1),(7,1)]

isPrefixOf

isPrefixOf 判断是否为前缀

1
2
3
4
ghci> "hey" `isPrefixOf` "hey there!"  
True
ghci> "hey" `isPrefixOf` "oh hey there!"
False

isInfixOf

isInfixOf 判断是否为中缀

1
2
3
4
5
6
ghci> "cat" `isInfixOf` "im a cat burglar"  
True
ghci> "Cat" `isInfixOf` "im a cat burglar"
False
ghci> "cats" `isInfixOf` "im a cat burglar"
False

isSuffixOf

isSuffixOf 判断是否为后缀

1
2
3
4
ghci> "there!" `isSuffixOf` "oh hey there!"  
True
ghci> "there!" `isSuffixOf` "oh hey there"
False

lines

lines 将字符串拆为多行

1
2
ghci> lines "first line\nsecond line\nthird line"  
["first line","second line","third line"]

unlines

unlineslines 相反

1
2
ghci> unlines ["first line", "second line", "third line"]  
"first line\nsecond line\nthird line\n"

words

words 将字符串拆成多个词组

1
2
3
4
ghci> words "hey these are the words in this sentence"  
["hey","these","are","the","words","in","this","sentence"]
ghci> words "hey these are the words in this\nsentence"
["hey","these","are","the","words","in","this","sentence"]

unwords

unwordswords 相反

1
2
ghci> unwords ["hey","there","mate"]  
"hey there mate"

nub

nub 移除重复元素,获得最小核心

1
2
3
4
ghci> nub [1,2,3,4,3,2,1,2,3,4,3,2,1]  
[1,2,3,4]
ghci> nub "Lots of words and stuff"
"Lots fwrdanu"

delete

delete 移除第一个指定元素

1
2
3
4
5
6
ghci> delete 'h' "hey there ghang!"  
"ey there ghang!"
ghci> delete 'h' . delete 'h' $ "hey there ghang!"
"ey tere ghang!"
ghci> delete 'h' . delete 'h' . delete 'h' $ "hey there ghang!"
"ey tere gang!"

\

\\ 去除共同部分

1
2
3
4
ghci> [1..10] \\ [2,5,9]  
[1,3,4,6,7,8,10]
ghci> "Im a big baby" \\ "big"
"Im a baby"

union

union 集合并

1
2
3
4
ghci> "hey man" `union` "man what's up"  
"hey manwt'sup"
ghci> [1..7] `union` [5..10]
[1,2,3,4,5,6,7,8,9,10]

intersect

intersect 集合交

1
2
ghci> [1..7] `intersect` [5..10]  
[5,6,7]

fst

fst 获取二元组的第一个元素

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

snd

snd 获取二元组的第二个元素

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

flip

flip 返回函数的翻转版本

1
2
3
4
ghci> flip zip [1,2,3,4,5] "hello"  
[('h',1),('e',2),('l',3),('l',4),('o',5)]
ghci> zipWith (flip' div) [2,2..] [10,8,6,4,2]
[5,4,3,2,1]

when

when 相当于没有 elseif

1
2
3
4
5
6
7
import Control.Monad   

main = do
c <- getChar
when (c /= ' ') $ do
putChar c
main

sequence

sequence 执行一系列的输入输出操作

1
2
3
4
5
6
7
8
9
main = do  
a <- getLine
b <- getLine
c <- getLine
print [a,b,c]
-- 相当于
main = do
rs <- sequence [getLine, getLine, getLine]
print rs

mapM

mapM 先进行 map 再遍历返回的 Monad

1
2
3
4
5
6
7
8
9
10
11
12
13
ghci> sequence (map print [1,2,3,4,5])  
1
2
3
4
5
[(),(),(),(),()]
-- 相当于
ghci> mapM print [1,2,3]
1
2
3
[(),(),()]

mapM_

mapM_ 类似 mapM 只是不关心返回的结果

1
2
3
4
ghci> mapM_ print [1,2,3]  
1
2
3