iterate f x == [x, f x , f (f x), ….]
element :: Int -> [Int] -> Bool
element x [] = False
element x (y : ys)
| x == y = True
|otherwise = element x ys
上式中的Int可以替换成Integer, Float等。
– 定义一般的函数
–Eq是一个class,是支持“==”的所有类型的集合
element :: Eq a => a -> [a] -> Bool
element x [] = False
element x (y : ys)
| x == y = True
| otherwise = element x ys
{-
class Eq a where
(==) :: a -> a -> Bool
(/=) :: a -> a -> Bool
(/=) x y = not (x == y)
-}
–Int,Float,Bool等都是Eq的实例(instance)
–class, instance都是Haskell实现重载的机制(overload)
class Sizable a where
size :: a -> Int
instance Sizable Int where
size :: Int -> Int
size x = x
instance Sizable Bool where
size :: Bool -> Int
size True = 1
size False = 0
instance Sizable Integer where
size :: Integer -> Int
size x = fromInteger x
–声明列表的定义
instance Sizable [a] where
size xs = sum (map size xs) –代替 [size x | x <- xs]
hasSize :: Sizable a => a -> Int
hasSize x = size x
{-
hasSize1 :: Int -> Int
hasSize1 x = x
hasSize2 :: Bool -> Int
hasSize2 True = 1
hasSize2 False = 0
hasSize3 :: Integer -> Int
hasSize3 x = fromInteger x
-}
–自定义类型
–如何表示一个星期的7天,一年的四季
data Bool = True | False
data Season = Spring | Summer | Fall | Winter
instance Show Season where
show Spring = “Spring”
show Summer = “Summer”
show Fall = “Fall”
show Winter = “Winter”
instance Eq Season where
Spring == Spring = True
Summer == Summer = True
Fall == Fall = True
Winter == Winter = True
isHot :: Season -> Bool
isHot Spring = True
isHot Summer = True
isHot Fall = True
isHot Winter = False
–“winter”, “chun” ++ “dong”
data Week = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday deriving (Show, Eq)
–instance Show Week where
–show Monday = “Monday”
–deriving Show代替(instance Show Week where)将Monday, Tuesday……转换为String类型
instance Show Week where
isWorkDay :: Week -> Bool
isWorkDay Saturday = False
isWorkDay Sunday = False
isWorkDay _ = True
print x = putStr (show x)
–定义一个表示人的类型People,包括姓名,性别和年龄
–给出People的定义,并给出一个值
type Name = String
type Age = Int
data Sex = Male | Female deriving (Show, Eq)
–type People = (Name, Age, Sex)
–(“Gao, 23, Male) :: People
data People = Person Name Sex Age
instance Show People where
show (Person name swx age) = name ++ “,” ++ show sex ++ “,” ++ show age
–Male, Female, Person是构造符constructor
–[a]
data list a = Nil | Cons a (List a)
–Nil, Cons x Nil, Cons x (Cons x Nil)
instance Show (List a) where
show Nil = “[]”
show (Cons x xs) = show x ++ “:” ++ show xs
–应用:输入人名,返回其电话号码
type PhoneNumber = String
type PhoneBook = [(Name, PhoneNumber)]
data Maybe a = Nothing | Just a
–Nothing, Just 2 :: Maybe Int, Nothing, Just True, Just False :: Maybe Bool
lookUp :: PhoneBook -> Name -> PhoneNumber
lookUp [] name = Nothing –递归基
lookUp ((n, p): ps) name
| n == name = Just p
| otherwise = lookUp ps name
–储存名字和电话号码
myPhoneBook = [(“Gao”, “1232”), (“Wang”, “2323”)]
foo :: Maybe a -> a
foo (Just x) = x –foo is not total
–foo Nothing = ?
–给定一个表达式,如7*4+2,计算其值
data Expr = Num Int | Add Expr Expr | Mul Expr Expr
–任务:instance Show Expr, instance Eq Expr
– Num 2, Add (Num 2) (Num 2), Mul (Num 2)(Num 3) , Add (Mul (Num 7)(Num 4)) (Num 2)
eval :: Expr -> Int
eval (Num n) = n
eval (Add e1 e2) = (eval e1) + (eval e2)
eval (Mul e1 e2) = (eval e1) * (eval e2)