Haskell 代码片段
一些零碎的代码,摘自 《 Haskell 趣学指南》,包含常用函数等。
succ
succ
返回后继
1 | ghci> succ 8 |
max
max
返回最大值
1 | ghci> max 100 101 |
min
min
返回最小值
1 | ghci> min 9 10 |
!!
!!
按索引获取元素
1 | ghci> "Steve Buscemi" !! 6 |
head
head
取列表的第一个元素
1 | ghci> head [5,4,3,2,1] |
tail
tail
取列表除了第一个元素的所有元素
1 | ghci> tail [5,4,3,2,1] |
last
last
取列表的最后一个元素
1 | ghci> last [5,4,3,2,1] |
init
init
取列表除了最后一个元素的所有元素
1 | ghci> init [5,4,3,2,1] |
inits
inits
重复取 init
1 | ghci> inits "w00t" |
tails
tails
重复取 tail
1 | ghci> tails "w00t" |
length
length
获取列表的长度
1 | ghci> length [5,4,3,2,1] |
null
null
判断列表是否为空
1 | ghci> null [1,2,3] |
reverse
reverse
反转一个列表
1 | ghci> reverse [5,4,3,2,1] |
take
take
拿取一定数量的元素
1 | ghci> take 3 [5,4,3,2,1] |
drop
drop
抛弃一定数量的元素
1 | ghci> drop 3 [8,4,2,1,5,6] |
maximum
maximum
返回列表最大值
1 | ghci> maximum [1,9,2,3,4] |
minimum
minimum
返回列表最小值
1 | ghci> minimum [8,4,2,1,5,6] |
sum
sum
列表求和
1 | ghci> sum [5,2,1,6,3,2,5,7] |
product
product
列表求积
1 | ghci> product [6,2,1,2] |
elem
elem
判断元素是否存在
1 | ghci> 4 `elem` [3,4,5,6] |
elemIndex
elemIndex
返回第一个指定元素索引
1 | ghci> :t elemIndex |
elemIndices
elemIndices
返回所有指定元素索引
1 | ghci> ' ' `elemIndices` "Where are the spaces?" |
find
find
查找符合条件的元素
1 | ghci> find (>4) [1,2,3,4,5,6] |
findIndex
findIndex
返回符合条件的元素索引
1 | ghci> findIndex (==4) [5,3,2,1,6,4] |
cycle
cycle
循环拼接列表
1 | ghci> take 10 (cycle [1,2,3]) |
repeat
repeat
重复指定元素
1 | ghci> take 10 (repeat 5) |
zip
zip
压缩函数,类似拉链
1 | ghci> zip [1,2,3,4,5] [5,5,5,5,5] |
zipWith
zipWith
用指定的方式 zip
1 | ghci> zipWith (+) [4,2,5,6] [2,6,2,3] |
zip3
zip3
是 zip
的 3 个参数版本
1 | ghci> zip3 [2,3,3] [2,2,2] [5,5,3] |
zipWith3
zipWith3
是 zipWith
的 3 个参数版本
1 | ghci> zipWith3 (\x y z -> x + y + z) [1,2,3] [4,5,2,2] [2,2,3] |
map
map
对列表每个元素应用函数
1 | ghci> map (+3) [1,5,3,1,6] |
filter
filter
过滤出列表中复合条件的元素
1 | ghci> filter (>3) [1,5,3,2,1,6,4,3,2,1] |
takeWhile
takeWhile
一直 take
直到条件不满足
1 | ghci> takeWhile (/=' ') "elephants know how to party" |
dropWhile
dropWhile
一直 drop
直到条件不满足
1 | ghci> dropWhile (/=' ') "This is a sentence" |
intersperse
intersperse
在列表元素之间插入指定元素
1 | ghci> intersperse '.' "MONKEY" |
intercalate
intercalate
将一个列表插入到另一个列表元素之间
1 | ghci> intercalate " " ["hey","there","guys"] |
transpose
transpose
转置矩阵
1 | ghci> transpose [[1,2,3],[4,5,6],[7,8,9]] |
concat
concat
拍扁列表
1 | ghci> concat ["foo","bar","car"] |
concatMap
concatMap
先 map
后 concat
1 | ghci> concatMap (replicate 4) [1..3] |
and
and
返回列表是否全是 true
1 | ghci> and $ map (>4) [5,6,7,8] |
or
or
返回列表是否存在 true
1 | ghci> or $ map (==4) [2,3,4,5,6,1] |
any
any
返回列表是否存在元素满足条件
1 | ghci> any (==4) [2,3,5,6,1,4] |
all
all
返回列表是否全部元素满足条件
1 | ghci> all (>4) [6,9,10] |
iterate
iterate
返回重复迭代的结果
1 | ghci> take 10 $ iterate (*2) 1 |
splitAt
splitAt
在指定位置分隔数组
1 | ghci> splitAt 3 "heyman" |
span
span
将列表分割为满足条件的列表和不满足条件的列表
1 | ghci> span (/=4) [1,2,3,4,5,6,7] |
break
break
在满足条件时分割列表
1 | ghci> break (==4) [1,2,3,4,5,6,7] |
partition
partition
类似 span
和 break
不过会对整个列表进行过滤。
1 | ghci> partition (`elem` ['A'..'Z']) "BOBsidneyMORGANeddy" |
sort
sort
列表排序
1 | ghci> sort [8,5,3,2,1,6,4,2] |
group
group
列表分组
1 | ghci> group [1,1,1,1,2,2,2,2,3,3,2,2,2,5,6,7] |
isPrefixOf
isPrefixOf
判断是否为前缀
1 | ghci> "hey" `isPrefixOf` "hey there!" |
isInfixOf
isInfixOf
判断是否为中缀
1 | ghci> "cat" `isInfixOf` "im a cat burglar" |
isSuffixOf
isSuffixOf
判断是否为后缀
1 | ghci> "there!" `isSuffixOf` "oh hey there!" |
lines
lines
将字符串拆为多行
1 | ghci> lines "first line\nsecond line\nthird line" |
unlines
unlines
与 lines
相反
1 | ghci> unlines ["first line", "second line", "third line"] |
words
words
将字符串拆成多个词组
1 | ghci> words "hey these are the words in this sentence" |
unwords
unwords
与 words
相反
1 | ghci> unwords ["hey","there","mate"] |
nub
nub
移除重复元素,获得最小核心
1 | ghci> nub [1,2,3,4,3,2,1,2,3,4,3,2,1] |
delete
delete
移除第一个指定元素
1 | ghci> delete 'h' "hey there ghang!" |
\
\\
去除共同部分
1 | ghci> [1..10] \\ [2,5,9] |
union
union
集合并
1 | ghci> "hey man" `union` "man what's up" |
intersect
intersect
集合交
1 | ghci> [1..7] `intersect` [5..10] |
fst
fst
获取二元组的第一个元素
1 | ghci> fst (8,11) |
snd
snd
获取二元组的第二个元素
1 | ghci> snd (8,11) |
flip
flip
返回函数的翻转版本
1 | ghci> flip zip [1,2,3,4,5] "hello" |
when
when
相当于没有 else
的 if
1 | import Control.Monad |
sequence
sequence
执行一系列的输入输出操作
1 | main = do |
mapM
mapM
先进行 map
再遍历返回的 Monad
1 | ghci> sequence (map print [1,2,3,4,5]) |
mapM_
mapM_
类似 mapM
只是不关心返回的结果
1 | ghci> mapM_ print [1,2,3] |