1灿巧, haskell是靜態(tài)類型語言,大部分情況依靠類型推斷而不需要顯示指定類型。
-- Int類型32bit或64bit丈探,超過范圍返回0
x :: Int
x= 2
-- Integer范圍無限
y :: Integer
y= 2
letter :: Char
letter = 'a'
interestRate :: Double
interestRate = 0.375
isFun :: Bool
isFun = True
-- list類型元素必須是同一種類型
values :: [Int]
values = [1, 2, 3]
testScores :: [Double]
testScores = [0.99, 0.7, 0.8]
letters :: [Char]
letters = ['a', 'b', 'c']
-- String本質(zhì)上就是[Char]類型
aPet :: [Char]
aPet = "cat"
anotherPet :: String
anotherPet = "dog"
-- tuple可以包含不同類型的元素
ageAndHeight ::(Int, Int)
ageAndHeight = (34, 74)
firstLastMiddle :: (String, String, Char)
firstLastMiddle = ("Oscar", "Grouch", 'D')
streetAddress :: (Int, String)
streetAddress = (123, "Happy St.")
-- 函數(shù)類型,haskell不支持自動類型轉(zhuǎn)換
half :: Int -> Double
half n = (fromIntegral n) / 2
-- show把任意類型轉(zhuǎn)成字符串
main :: IO ()
main = print $ show 6
-- read把字符串類型轉(zhuǎn)為其他類型
number = read "6" :: Int
anotherNumber :: Int
anotherNumber = read "6"
-- 多參數(shù)函數(shù)崭孤,最后一個是返回值類型
makeAddress :: Int -> String -> String -> (Int, String, String)
makeAddress number street town = (number, street, town)
-- partial application
makeAddressLambda = (\number ->
(\street ->
(\town -> (number, street, town)))
-- 函數(shù)參數(shù)類型加小括號
ifEven :: (Int -> Int) -> Int -> Int
ifEven f n = if even n
then f n
else n
-- 類型聲明中任何小寫字符都可以表示任意類型(類似泛形)
simple :: a -> a
simple x = x
-- 相同字母表示同一種類型类嗤,不同字母可以表示同一種類型
makeTriple :: a -> b -> c -> (a, b, c)
makeTriple x y z = (x, y, z)
2, 自定義類型
2.1 type定義類型別名辨宠,相當于c++中的typedef/using
patientInfo :: String -> String -> Int -> Int -> String
patientInfo fname lname age height = name ++ " " ++ ageHeight
where name = lname ++ ", " ++ fname
ageHeight = "(" ++ show age ++ "yrs. " ++ show height ++ "in.)"
type FirstName = String
type LastName = String
type Age = Int
type Height = Int
patientInfo :: FirstName -> LastName -> Age -> Height -> String
2.2 data自定義類型(相當于c中的struct)
自定義類型.png
data是關鍵字遗锣,=號左邊是類型構(gòu)造,可以帶參數(shù)定義泛形類型嗤形。=號右邊是數(shù)據(jù)構(gòu)造精偿,用于構(gòu)造類型實例,多個數(shù)據(jù)構(gòu)造用|分割赋兵。類型構(gòu)造和數(shù)據(jù)構(gòu)造的名稱不要求相同笔咽。
type FirstName = String
type MiddleName = String
type LastName = String
-- 數(shù)據(jù)構(gòu)造名稱后面是參數(shù)類型
data Name = Name FirstName LastName
| NameWithMiddle FirstName MiddleName LastName
-- 模式匹配
showName :: Name -> String
showName (Name f l) = f ++ " " ++ l
showName (NameWithMiddle f m l) = f ++ " " ++ m ++ " " ++ l
name1 = Name "Jerome" "Salinger"
name2 = NameWithMiddle "Jerome" "David" "Salinger"
showName name1 = "Jerome Salinger"
showName name2 = "Jerome David Salinger"
-- record語法,用于字段較多的情況
data Patient = Patient { name :: Name
, sex :: Sex
, age :: Int
, height :: Int
, weight :: Int
, bloodType :: BloodType }
jackieSmith :: Patient
jackieSmith = Patient {name = Name "Jackie" "Smith"
, age = 43
, sex = Female
, height = 62
, weight = 115
, bloodType = BloodType O Neg }
-- getter:每個字段的類型都是:字段所在類型 -> 字段類型
height jackieSmith = 62
-- setter:返回一個新對象
jackieSmithUpdated = jackieSmith { age = 44 }
3霹期,type classes(相當于java中的interface)
type classes.png
-- 定義type classes
class Eq a where
(==) :: a -> a -> Bool
(/=) :: a -> a -> Bool
-- Ord繼承Eq
class Eq a => Ord a where
compare :: a -> a -> Ordering
(<) :: a -> a -> Bool
(<=) :: a -> a -> Bool
(>) :: a -> a -> Bool
(>=) :: a -> a -> Bool
max :: a -> a -> a
min :: a -> a -> a
-- 調(diào)用type classes函數(shù)
min 1 2 = 2
-- type classes可以定義字段叶组,相當于無參函數(shù)
class Bounded a where
minBound :: a
maxBound :: a
-- 訪問type classes的字段需要指定實現(xiàn)類
minBound :: Int = -9223372036854775808
-- 自動實現(xiàn)內(nèi)置的type classed(相當于java中從Object繼承equals和toString)
data Icecream = Chocolate | Vanilla deriving (Show, Eq, Ord)
-- type classes多繼承?历造?甩十?
-- type classes函數(shù)默認實現(xiàn)船庇??侣监?
type實現(xiàn)type classed
class Show a where
show :: a -> String
data SixSidedDie = S1 | S2 | S3 | S4 | S5 | S6
instance Show SixSidedDie where
show S1 = "one"
show S2 = "two"
show S3 = "three"
show S4 = "four"
show S5 = "five"
show S6 = "six"
多態(tài):
read.png
核心type classes層次結(jié)構(gòu)
type classes層次結(jié)構(gòu).png