Haskell学习笔记&函数库

数值类型及常见符号

类型 意义
Int 整数(32位)
Integer 任意精度整数
String 字符串
Char 字符型(包括n——换行符)
Float 实数(单精度浮点数)
Double 双精度浮点数
Bool 布尔值(True or False)
List 列表
[a] 列表类型,表示a的值的序列类型(a是一个类型)
Eq 可判断相等性的类型
Ord 可比较大小的类型
Show 可用字符串表示的类型
Read 可将字符串转化为数字或Bool的类型

Haskell函数命名方式

例:add :: (Int,Int) -> Int
add (x,y) = x + y
例:add :: Int ->Int -> Int
add x y = x + y
例:div a b :: Int -> Int – > Int
div a b
(空格)| b = 0 = “Error”
(空格)| otherwise = a / b
例:div a b = function
(空格)where
(空格)function = a / b

常用命令

++:运算符,强制连接。(例:”a”++”b” = “ab”)
max a b :最大值
min a b:最小值
If x > y then x else y :判断语句(必须有else)
:t :”:type”缩写,显示函数数值类型
length(“字符串”):求字符串长度
length [1,2,3….] :获取List元素个数
元素:数组:将元素加入数组首位(例:1:[2,3] = [1,2,3])
head [a] 取出数组的第一个元素
tail [a] 去掉数组的第一个元素
last [a] 取出数组最后一个元素
init [a] 去掉数组最后一个元素
zip [a] [b] :将两List合并为一个二元数组的List(例:zip [a,b,c] [1,2,3] = [(a,1),(b,2),(c,3)])
abs a:绝对值
sum [a]:求该list中元素的和
product [a]:求该list中元素的积
div a b :整除
gcd a b:最大公约数
mod a b:余数
=/ 不等于
== 相等
type 变量 = 类型 :定义一个变量的类型
data 变量名 = 数值情况 or 变量类型情况
fst、snd:取出一个二元Trple(数组)的第一、第二个元素
iterate 参数 值 :以值为x,无限循环f(x)
map 公式或函数 List :将List中每个数通过函数计算返回一个新的List(例:map (+2) [1,2,3] = [3,4,5])
double [List] :返回一个List中的偶数
reverse [List] :反转一个List
putStrLn “xxxx”:输出
sequenceA [[a],[b]….]:返回[a],[b]中元素结合的所有情况(Data.Traversable)
(例:sequenceA [[1,2,3],[4,5,6]] = [[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]])

注意事项

1、变量首字母不能大写。
2、List只允许放置同一类型的数据。
3、[Int]可以是[]、[3,4]、[1,2,3….]等。
4、String相当于[Char]。
5、查看运算符类型(:t)需要括号。(例::t (||) )
6、[[Int]]代表[Int]的列表
7、[1..10]=[1,2,3,4,5,6,7,8,9,10]
8、中缀函数需要在函数两侧加`。
9、List比较大小规则:两个List第一个元素比较,第一个相同则比较第二个,以此类推。
10、x:xs 表示将List头部绑定为x。尾部绑定为xs,若只有一个元素,则xs表示空List。
11、Haskell的惰性特征使得它”使用多少,计算多少“,不存在[1….]等无穷序列死循环的问题

三种类型的函数:
add :: Int -> Int -> Int
add x y = x + y
mylength :: [a] -> Int
mylength [] = 0
mylength (x : xs) = 1 + mylength xs
myElement :: Eq a => a -> [a] -> Bool
myElement x [] = False
myElement x (y : ys) = x == y || myElement x ys

Prelude函数库

数学函数:
div :: Integral a => a -> a -> a 整除
mod :: Integral a => a -> a -> a 取模

乘方函数
(^) :: (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 “abcn123ndefn” –分割行
[“abc”,”123″,”def”]
Prelude> words “abcn123 deft” –分割单词
[“abc”,”123″,”def”]
Prelude> unlines [“abc”, “123”, “def”] –合并行
“abcn123ndefn”
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 ()

未经允许不得转载:MikuAlpha's Blog » Haskell学习笔记&函数库

赞 (1)

评论 0

  • 昵称 (必填)
  • 邮箱 (选填)