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)]