《Haskell趣学指南》笔记之函数

系列文章缓存


模式匹配

写好模式,Haskell 会直接帮你匹配。如:函数

lucky :: Int -> String
lucky 7 = "7 is the lucky number!"
lucky x = "sorry, you are not lucky"
复制代码

载入以后运行post

λ> lucky 2
"sorry, you are not lucky"
λ> lucky 3
"sorry, you are not lucky"
λ> lucky 7
"7 is the lucky number!"

复制代码

可是注意,若是把 lucky x 挪到 lucky 7 前面,就永远匹配不到 lucky 7 了。spa

  • 用模式匹配实现阶乘:
factorial :: Int -> Int 
factorial 0 = 1 
factorial n = n * factorial (n - 1)
复制代码
  • 定义模式的时候,必定在最后留一个万能模式,不然可能会出错
  • 元组的模式匹配
addVectors :: (Double, Double) -> (Double, Double) -> (Double, Double) 
addVectors a b = (fst a + fst b, snd a + snd b)
-- 能够改写为
addVectors :: (Double, Double) -> (Double, Double) -> (Double, Double) 
addVectors (x1, y1) (x2, y2) = (x1 + x2, y1 + y2)
复制代码
  • 模式中能够用 _ 来占位,这个 _ 叫作泛变量
  • 能够用 x: _ 来匹配列表的第一个元素 x,可是要记得加圆括号,否则 Haskell 没法理解它
head' :: [a] -> a 
head' [] = error "Can' t call head on an empty list, dummy!" -- error 会中断程序
head' (x:_) = x -- 这里的圆括号不是 tuple 的标记
复制代码
  • 匹配只有一个元素的列表,能够用 [x],也能够用 (x:[])
  • 匹配只有两个元素的列表,能够用 [x,y],也能够用 (x:y:[])
  • 匹配大于两个元素的列表,用 (x:y:_),不能用 [] 的形式
  • as 模式:all@(x:items) 中的 all 表示整个 x:items 列表,方便后面引用

守卫/哨卫 guard

bmiTell :: Double -> String 
bmiTell weight height    
    | weight / height ^ 2 <= 18.5 = putStrLn "你体重不足,就像根竹竿!"
    | weight / height ^ 2 <= 25.0 = putStrLn "你体重正常,那你确定是个丑逼!" &emsp; &emsp;
    | weight / height ^ 2 <= 30.0 = putStrLn "你体重超过,快减肥吧肥宅!" &emsp; &emsp;
    | otherwise &emsp;                      = putStrLn "你是猪!"
复制代码

其中的 bmi < 18.5 就是一个 guard。 每条 guard 语句至少缩进一个空格。 若是当前模式的守卫都没有 True,并且也没有写 otherwise,就会进入下一个模式。3d

where

可使用 where 来对计算结果进行缓存,也能够定义其余的帮助函数:code

bmiTell :: Double -> String 
bmiTell weight height &emsp; &emsp;
    | bmi <= 18.5 = putStrLn "你体重不足,就像根竹竿!"
    | bmi <= 25.0 = putStrLn "你体重正常,那你确定是个丑逼!" &emsp; &emsp;
    | bmi <= 30.0 = putStrLn "你体重超过,快减肥吧肥宅!" &emsp; &emsp;
    | otherwise     = putStrLn "你是猪!"
    where bmi = weight / height ^ 2
             x = "whatever"
             getBmi weight height = weight / height ^ 2
复制代码

where 只在当前模式中有效。ci

就算没有 guard,也能够在函数定义中使用 where:get

calcBmis :: [(Double, Double)] -> [Double] 
calcBmis xs = [bmi w h | (w, h)< - xs] &emsp; &emsp; 
    where bmi weight height = weight / height ^ 2
复制代码

what 还能这样用string

describeList :: [a] -> String
describeList ls = "The list is " ++ what ls
    where what [] = "empty."
             what [x] = "a singleton list."
             what xs = "a longer list."
复制代码

其中 what ls 是以 ls 为参数调用 what 函数。it

let

ghci> 4 * (let a = 9 in a + 1) + 2 
42
复制代码

let 和 where 类似,不一样点在于 where 只容许咱们在函数底部绑定变量,且对当前模式可见。而 let 能够出如今任何地方,且 let 里面的变量只在 in 内有效。

另外一个区别就是 let 是表达式(有值),而 where 不是。

case

case <exp> of pattern1 -> result1
                    pattern2 -> result2
                    pattern3 -> result3
复制代码

模式匹配不过是 case 的语法糖,并且模式匹配只能用在函数定义里,而 case 能够用在任何地方。

相关文章
相关标签/搜索