haskell笔记(1)
2016-10-12 20:26
176 查看
myElem :: Int-> [Int] -> Bool
myElem x [] = False
myElem x (y : ys)
|x==y = True
|otherwise = myElem x ys
重载函数(overloaded)
myElem :: Eq a => a -> [a] -> Bool
myElem x [] = False
myElem x (y : ys)
|x==y = True
|otherwise = myElem x ys等价于
myElem x (y : ys) = (x==y) || myElem x ys
普通函数(单态)
add :: Int -> Int -> Int
add x y = x + y
多态函数(polymorphic)
length :: [a] -> Int –多态类型,多态函数
length [] = 0
length (x : xs) = length xs + 1
–一个图书馆的借书程序
type Person = String –name
type Book = String –name of the book
tyoe Database = [(Book,Person)]
借书
makeLoan :: DataBase -> Person -> Book -> DataBase
makeLoan db person book = (book,person) : db
–db ++ [(book,person)]
还书
returnLoan :: Database -> Person -> Book -> Database
returnLoad db person book = [pair | pair <- db, pair /= (book,person)]
myElem x [] = False
myElem x (y : ys)
|x==y = True
|otherwise = myElem x ys
重载函数(overloaded)
myElem :: Eq a => a -> [a] -> Bool
myElem x [] = False
myElem x (y : ys)
|x==y = True
|otherwise = myElem x ys等价于
myElem x (y : ys) = (x==y) || myElem x ys
普通函数(单态)
add :: Int -> Int -> Int
add x y = x + y
多态函数(polymorphic)
length :: [a] -> Int –多态类型,多态函数
length [] = 0
length (x : xs) = length xs + 1
–一个图书馆的借书程序
type Person = String –name
type Book = String –name of the book
tyoe Database = [(Book,Person)]
借书
makeLoan :: DataBase -> Person -> Book -> DataBase
makeLoan db person book = (book,person) : db
–db ++ [(book,person)]
还书
returnLoan :: Database -> Person -> Book -> Database
returnLoad db person book = [pair | pair <- db, pair /= (book,person)]
相关文章推荐
- Haskell 笔记(二)List和元组
- 【笔记】Haskell
- haskell笔记1
- Haskell语言学习笔记(16)Alternative
- Haskell语言学习笔记(37)RWS, RWST
- Haskell语言学习笔记(14)Foldable
- programming in haskell 读书笔记1
- Haskell语言学习笔记(19)File IO
- Haskell语言学习笔记(31)ListT
- Haskell学习笔记 --- 基础语法篇二
- Haskell学习笔记 --- 函数式编程中的柯里化
- Haskell语言学习笔记(7)IO 与随机数
- Haskell语言学习笔记(23)MonadReader, Reader, ReaderT
- haskell 笔记2
- Haskell语言学习笔记(13)Monadic Functions
- Haskell语言学习笔记(28)Data.Map
- Haskell语言学习笔记(32)Data.Maybe, Data.Either
- Haskell笔记(6)
- Haskell语言学习笔记(38)Lens(1)
- haskell 乱搞笔记[原创]