
Haskell
Haskell Prelude 函数
转载自 .html
转载自
数学函数:
加、减、乘、除、取余函数
(+)
:: Num a => a -> a -> a
(-)
:: Num a => a -> a -> a
(*)
:: Num a => a -> a -> a
(/)
:: Fractional a => a -> a -> a
div
:: Integral a => a -> a -> a
mod
:: Integral a => a -> a -> a
divMod
:: Integral a => a -> a -> (a, a)
quot
:: Integral a => a -> a -> a
rem
:: Integral a => a -> a -> a
quotRem
:: Integral a => a -> a -> (a, a)
注意:/ 函数的操作数是小数
其中divMod和quotRem的区别是:前者余数的符号与除数相同,而后者与被除数相同
Prelude> 1 + 2
3
Prelude> 2 - 3
-1
Prelude> 3 * 4
12
P
Prelude> 5 / 2
2.5
Prelude> (+) 1 2
3
Prelude> (-) 2 3
-1
Prelude> (*) 3 4
12
Prelude> (/) 5 2
2.5
Prelude> divMod 9 (-2)
(-5,-1)
Prelude> quotRem 9 (-2)
(-4,1)
乘方函数
(^)
:: (Num a, Integral b) => a -> b -> a
(^^)
:: (Fractional a, Integral b) => a -> b -> a
(**)
:: Floating a => a -> a -> a
三个函数不同在于操作数
^ 的底数可为小数,也可为整数,指数是正整数;
^^ 的底数是小数,指数是任意整数;
** 的底数和指数都是小数)
Prelude> 2 ^ 3
8
Prelude> 2.5 ^ 3
15.625
Prelude> 2.5 ^^ 3
15.625
Prelude> 2.5 ** 3.5
24.705294220065465
逻辑运算函数:
not
:: Bool -> Bool
(&&)
:: Bool -> Bool -> Bool
(||)
:: Bool -> Bool -> Bool
(==)
:: Eq a => a -> a -> Bool
(/=)
:: Eq a => a -> a -> Bool
(<)
:: Ord a => a -> a -> Bool
(<=)
:: Ord a => a -> a -> Bool
(>)
:: Ord a => a -> a -> Bool
(>=)
:: Ord a => a -> a -> Bool
Prelude> not True
False
Prelude> True && False
False
Prelude> 1 == 1
True
Prelude> 1 /= 1
False
Prelude> 1 < 2 || 2 <= 3
True
数值函数:
signum
:: Num a => a -> a
negate
:: Num a => a -> a
abs
:: Num a => a -> a
recip
:: Fractional a => a -> a
floor
:: (RealFrac a, Integral b) => a -> b
ceiling
:: (RealFrac a, Integral b) => a -> b
round
:: (RealFrac a, Integral b) => a -> b
truncate
:: (RealFrac a, Integral b) => a -> b
exp
:: Floating a => a -> a
subtract
:: Num a => a -> a -> a
gcd
:: (Integral a) => a -> a -> a
lcm
:: Integral a => a -> a -> a
sqrt
:: Floating a => a -> a
min
:: Ord a => a -> a -> a
max
:: Ord a => a -> a -> a
compare
:: Ord a => a -> a -> Ordering
Prelude> signum (-3)
--符号
-1
Prelude> negate (-3)
--相反数
3
Prelude> abs (-3)
--绝对值
3
Prelude> recip 3
--倒数
0.3333333333333333
Prelude> floor 3.8
--向下舍入
3
Prelude> ceiling 3.2
--向上舍入
4
Prelude> round 3.5
--四舍五入
4
Prelude> truncate 3.8
--取整函数
3
Prelude> exp 1
-- e 的 x 的次方
2.718281828459045
Prelude> subtract 3 5
--减去
2
Prelude> gcd 3 9
--最大公约数
3
Prelude> lcm 3 9
--最小公倍数
9
Prelude> sqrt 2
--开方
1.4142135623730951
Prelude> min 3 5
--较小者
3
Prelude> max 3 5
--较大者
5
Prelude> compare 3 5
--比较
LT
--less then
Prelude> compare 5 3
GT
--greater then
Prelude> compare 5 5
EQ
--equal
三角函数:
pi
:: Floating a => a
sin
:: Floating a => a -> a
cos
:: Floating a => a -> a
tan
:: Floating a => a -> a
asin
:: Floating a => a -> a
acos
:: Floating a => a -> a
atan
:: Floating a => a -> a
atan2
:: RealFrac a => a -> a
sinh
:: Floating a => a -> a
cosh
:: Floating a => a -> a
tanh
:: Floating a => a -> a
asinh
:: Floating a => a -> a
acosh
:: Floating a => a -> a
atanh
:: Floating a => a -> a
对数函数:
log
:: Floating a => a -> a
logBase
:: Floating a => a -> a -> a
Prelude> log (exp 1)
1.0
Prelude> logBase 10 10
1.0
odd
:: Integral a => a -> Bool
even
:: Integral a => a -> Bool
Prelude> odd 3
--奇数
True
Prelude> even 3
--偶数
二元组函数:
fst
:: (a, b) -> a
snd
:: (a, b) -> b
Prelude> fst (1, 2)
--取第一个元素
1
Prelude> snd (1, 2)
--取第二个元素
2
列表函数:
(!!)
:: [a] -> Int -> a
lookup
:: Eq a => a -> [(a, b)] -> Maybe b
Prelude> [0, 1, 2, 3] !! 1
--获取列表中第几个元素
1
Prelude> lookup 2 [(1, 'a'), (2, 'b'), (3, 'c')]
--获取列表中第一个元素为2的元组中第二个元素
Just 'b'
elem
:: Eq a => a -> [a] -> Bool
notElem
:: Eq a => a -> [a] -> Bool
Prelude> elem 3 [1, 2, 3]
--判断元素是否在列表中
True
Prelude> notElem 3 [1, 2, 3]
False
null
:: [a] -> Bool
Prelude> null []
--判断列表是否为空
True
Prelude> null [1, 2, 3]
False
and
:: [Bool] -> Bool
or
:: [Bool] -> Bool
Prelude> and [True, False, True]
--逻辑 与 运算
False
Prelude> or [True, False, False]
--逻辑 或 运算
True
all
:: (a -> Bool) -> [a] -> Bool
any
:: (a -> Bool) -> [a] -> Bool
Prelude> all even [2, 4, 6, 8, 10]
--判断所有元素是否满足某一条件
True
Prelude> any odd [2, 4, 6, 8, 10]
--判断是否有元素满足某一条件
False
(++)
:: [a] -> [a] -> [a]
Prelude> [0, 1, 2] ++ [3, 4, 5]
--list 连接
[0,1,2,3,4,5]
length
:: [a] -> Int
Prelude> length [1, 2, 3]
--求列表长度
3
head
:: [a] -> a
tail
:: [a] -> [a]
last
:: [a] -> a
init
:: [a] -> [a]
Prelude> head [1, 2, 3, 4]
--取列表第一个元素
1
Prelude> tail [1, 2, 3, 4]
--取列表除了第一个元素的所有元素
[2,3,4]
Prelude> last [1, 2, 3, 4]
--取列表最后一个元素
4
Prelude> init [1, 2, 3, 4]
--取列表除了最后一个元素的所有元素
[1,2,3]
reverse
:: [a] -> [a]
cycle
:: [a] -> [a]
repeat
:: a -> [a]
replicate
:: Int -> a -> [a]
iterate
:: (a -> a) -> a -> [a] iterate (++ " ") "" = ["", " ", " ",...]
iterate f x
= x : iterate f (f x)
Prelude> reverse [1, 2, 3, 4]
--反转列表
[4,3,2,1]
Prelude> cycle [1, 2, 3, 4]
--反复出现列表
[1, 2, 3, 4,1, 2, 3, 4,1, 2, 3, 4,1, 2, 3, 4,1, 2, 3, 4, ...]
Prelude> repeat 1
--反复出现某一值
[1,1,1,1,1,1,1,1,1,1,1, ...]
Prelude> replicate 3 1
--重复出现某一值一定次数
[1, 1, 1]
take
:: Int -> [a] -> [a]
drop
:: Int -> [a] -> [a]
takeWhile
:: (a -> Bool) -> [a] -> [a]
dropWhile
:: (a -> Bool) -> [a] -> [a]
span
:: (a -> Bool) -> [a] -> ([a], [a])
break
:: (a -> Bool) -> [a] -> ([a], [a])
splitAt
:: Int -> [a] -> ([a], [a])
Prelude> take 2 [1, 2, 3, 4]
--获取前2个元素
[1,2]
Prelude> drop 2 [1, 2, 3, 4]
--丢弃前2个元素
[3,4]
Prelude> takeWhile even [2, 4, 5, 6]
--获取满足某一条件的前几个元素
[2,4]
Prelude> dropWhile even [2, 4, 5, 6]
--丢弃满足某一条件的前几个元素
[5,6]
Prelude> span even [2, 4, 5, 6]
--分割列表,规则同 takeWhile
([2,4],[5,6])
Prelude> break odd [2, 4, 5, 6]
--分割列表,规则同 dropWhile
([2,4],[5,6])
Prelude> splitAt 2 [2, 4, 5, 6]
--分割列表,规则同 take,drop
([2,4],[5,6])
lines
:: String -> [String]
words
:: String -> [String] words "ab d as+3" = ["ab","d","as+3"]
unlines
:: [String] -> String
unwords
:: [String] -> String
Prelude> lines "abc\n123\ndef\n"
--分割行
["abc","123","def"]
Prelude> words "abc\n123 def\t"
--分割单词
["abc","123","def"]
Prelude> unlines ["abc", "123", "def"]
--合并行
"abc\n123\ndef\n"
Prelude> unwords ["abc", "123", "def"]
--合并单词
"abc 123 def"
zip
:: [a] -> [b] -> [(a, b)]
zip3
:: [a] -> [b] -> [c] -> [(a, b, c)]
zipWith
:: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith3
:: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
unzip
:: [(a, b)] -> ([a], [b])
unzip3
:: [(a, b, c)] -> ([a], [b], [c])
Prelude> zip [1, 2, 3] [4, 5, 6]
[(1,4),(2,5),(3,6)]
Prelude> zip3 [1, 2, 3] [4, 5, 6] [7, 8, 9]
[(1,4,7),(2,5,8),(3,6,9)]
Prelude> zipWith (+) [1, 2, 3] [4, 5, 6]
[5,7,9]
Prelude> zipWith3 (\x y z -> x + y + z) [1, 2, 3] [4, 5, 6] [7, 8, 9]
[12,15,18]
Prelude> unzip [(1, 4), (2, 5), (3, 6)]
([1,2,3],[4,5,6])
Prelude> unzip3 [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
([1,2,3],[4,5,6],[7,8,9])
maximum
:: Ord a => [a] -> a
minimum
:: Ord a => [a] -> a
sum
:: Num a => [a] -> a sum [1,2,3] = 6
product
:: Num a => [a] -> a
Prelude> maximum [1, 2, 3]
--最大值
3
Prelude> minimum [1, 2, 3]
--最小值
1
Prelude> sum [1, 2, 3, 4]
--总和
10
Prelude> product [1, 2, 3, 4]
--总积
24
enumFrom
:: Enum a => a -> [a]
相当于 [n..]
enumFromThen
:: Enum a => a -> a -> [a]
相当于 [m,n..]
enumFromThenTo
:: Enum a => a -> a -> a -> [a]
相当于 [m,n..o]
enumFromTo
:: Enum a => a -> a -> [a]
相当于 [m..n]
show
:: Show a => a -> String (see 6.3.3)
read
:: Read a => String -> a
循环函数:
iterate
:: (a -> a) -> a -> [a]
iterate f x
= x : iterate f (f x)
until
:: (a -> Bool) -> (a -> a) -> a -> a
until p f x
= if p x then x else until p f (f x)
其他:
(.) :: (b -> c) -> (a -> b) -> a -> c
Function composition
(f . g) x
= f (g x)
($) :: (a -> b) -> a -> b
apply 函数,通常是为了省写括号
Prelude> putStrLn ("this is the first part >" ++ "< this is the second patr")
this is the first part >< this is the second patr
Prelude> putStrLn $ "this is the first part >" ++ "< this is the second patr"
this is the first part >< this is the second patr
Prelude> (map Char.toUpper . filter Char.isLower) "ABCdef"
"DEF"
Prelude> map Char.toUpper . filter Char.isLower $ "ABCdef"
"DEF"
seq
:: a -> b -> b
seq 函数强制先计算第一个参数,然后返回第二个参数
($!)
:: (a -> b) -> (a -> b)
同 $ 函数,只是不是 lazy 运算,属于 strict 运算
f $! x = x `seq` f x
map
:: (a -> b) -> [a] -> [b]
filter
:: (a -> Bool) -> [a] -> [a]
flip
:: (a -> b -> c) -> (b -> a -> c)
foldl
:: (a -> b -> a) -> a -> [b] -> a
foldl1
:: (a -> a -> a) -> [a] -> a
foldr
:: (a -> b -> b) -> b -> [a] -> b
foldr1
:: (a -> a -> a) -> [a] -> a
scanl
:: (a -> b -> a) -> a -> [b] -> [a]
scanl1
:: (a -> a -> a) -> [a] -> [a]
scanr
:: (a -> b -> b) -> b -> [a] -> [b]
scanr1
:: (a -> a -> a) -> [a] -> [a]
id
:: a -> a
id
x
= x
const
:: a -> b -> a
const k _
= k
asTypeOf
:: a -> a -> a
asTypeOf
= const
curry
:: ((a, b) -> c) -> a -> b -> c
uncurry
:: (a -> b -> c) -> (a, b) -> c
Prelude> curry fst 1 2
1
Prelude> uncurry (+) (1, 2)
3
concat
:: MonadPlus m => [m a] -> m a
concatMap
:: (a -> [b]) -> [a] -> [b]
Prelude> concat [[1, 2], [3, 4]]
[1,2,3,4]
Prelude> concatMap (\(x, y) -> [x*y]) [(1, 2), (3, 4), (5, 6)]
[2,12,30]
otherwise
:: Bool
Prelude> otherwise
--相当于 True
True
maybe
:: b -> (a -> b) -> Maybe a -> b
maybe 0 (+1) (Just 1) = 2
Prelude> maybe 0 (+1) (Nothing)
0
Prelude> maybe 0 (+1) (Just 2)
3
error
:: String -> a
undefined
:: a
undefined
= error "Prelude.undefined
succ
:: Enum a => a -> a
pred
:: Enum a => a -> a
Prelude> succ False
--后一个 Enum 值
True
Prelude> pred True
--前一个 Enum 值
False
知识所限,以后再整理
properFraction
:: (RealFrac a, Integral b) => a -> (b, a)
scaleFloat
:: RealFloat a => Int -> a -> a
significand
:: RealFloat a => a -> a
exponent
:: RealFloat a => a -> Int
isDenormalized
:: RealFloat a => a -> Bool
isIEEE
:: RealFloat a => a -> Bool
isInfinite
:: RealFloat a => a -> Bool
isNaN
:: RealFloat a => a -> Bool
isNegativeZero
:: RealFloat a => a -> Bool
fromInteger
:: Num a => Integer -> a (see 3.2)
fromIntegral
:: (Integral a, Num b) => a -> b
fromRational
:: Fractional a => Rational -> a (see 3.2)
fromEnum
:: Enum a => a -> Int
toEnum
:: Enum a => Int -> a toEnum 0 :: Bool = False
toInteger
:: Integral a => a -> Integer
toRational
:: Real a => a -> Rational
floatDigits
:: RealFloat a => a -> Int
floatRadix
:: RealFloat a => a -> Integer
floatRange
:: RealFloat a => a -> (Int, Int)
decodeFloat
:: RealFloat a => a -> (Integer, Int)
encodeFloat
:: RealFloat a => Integer -> Int -> a
fail
:: Monad m => String -> m a
fmap
:: Functor f => (a -> b) -> f a -> f b
lex
:: ReadS String lex "abc def" = [("abc"," def")]
maxBound
:: Bounded a => a
minBound
:: Bounded a => a
print
:: Show a => IO () adds a newline
putChar
:: Char -> IO ()
putStr
:: String -> IO ()
putStrLn
:: String -> IO () adds a newline
readFile
:: FilePath -> IO String
readIO
:: Read a => String -> IO a fails with IOError
readList
:: Read a => ReadS [a]
readLn
:: Read a => IO a
readParen
:: Bool -> ReadS a -> ReadS a
reads
:: Read a => ReadS a reads "1 2" :: [(Int,String)] = [(1," 2")]
readsPrec
:: Read a => Int -> ReadS a
realToFrac
:: (Real a, Fractional b) => a -> b
return
:: Monad m => a -> m a
sequence
:: Monad m => [m a] -> m [a]
sequence_
:: Monad m => [m a] -> m () do operations in sequence
showChar
:: Char -> ShowS
showList
:: Show a => [a] -> ShowS
showParen
:: Bool -> ShowS -> ShowS
showString
:: String -> ShowS
shows
:: Show a => a -> ShowS (see 6.3.3)
showsPrec
:: Show a => Int -> a -> ShowS (see 6.3.3)
getChar
:: IO Char eof generates an IOError
getContents
:: IO String
getLine
:: IO String eof generates an IOError
interact
:: (String -> String) -> IO ()
ioError
:: IOError -> IO a
userError
:: String -> IOError
writeFile
:: FilePath -> String -> IO ()
appendFile
:: FilePath -> String -> IO ()
applyM
:: Monad m => (a -> m b) -> m a -> m b
catch
:: IO a -> (IOError -> IO a) -> IO a
(=<<)
:: Monad a => (a -> m b) -> m a -> m b
Monadic binding (see 6.3.6)
(>>)
:: Monad m => m a -> m b -> m b Monadic binding (see 6.3.6)
(>>=)
:: Monad m => m a -> (a -> m b) -> m b Monadic binding (see 6.3.6)
mapM
:: Monad m => (a -> m b) -> [a] -> m [b]
mapM_
:: Monad m => (a -> m b) -> [a] -> m ()
本文标签:
Haskell
发表评论