Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell:绑定模式匹配的位置_Haskell_Pattern Matching_Pattern Guards - Fatal编程技术网

Haskell:绑定模式匹配的位置

Haskell:绑定模式匹配的位置,haskell,pattern-matching,pattern-guards,Haskell,Pattern Matching,Pattern Guards,目前,我正试图通过在线教程学习Haskell。在“函数中的语法”一章中,作者写道“您还可以使用模式匹配的where绑定!”。之后是代码示例的一部分,但我不知道模式匹配与新的where绑定一起使用在哪里。 因为代码块的第一部分被缩短了(“我们可以将前面函数的where部分重写为”),所以您只能推断它,但我认为我选择了正确的部分 职能: bmiTell :: (RealFloat a) => a -> a -> String bmiTell weight height

目前,我正试图通过在线教程学习Haskell。在“函数中的语法”一章中,作者写道“您还可以使用模式匹配的where绑定!”。之后是代码示例的一部分,但我不知道模式匹配与新的where绑定一起使用在哪里。 因为代码块的第一部分被缩短了(“我们可以将前面函数的where部分重写为”),所以您只能推断它,但我认为我选择了正确的部分

职能:

bmiTell :: (RealFloat a) => a -> a -> String  
bmiTell weight height  
    | bmi <= skinny = "You're underweight, you emo, you!"  
    | bmi <= normal = "You're supposedly normal. Pffft, I bet you're ugly!"  
    | bmi <= fat    = "You're fat! Lose some weight, fatty!"  
    | otherwise     = "You're a whale, congratulations!"  
    where bmi = weight / height ^ 2  
          skinny = 18.5  
          normal = 25.0  
          fat = 30.0
因为我想了解本教程中解释的Haskell的所有代码示例和语法方法,我希望有人能解释在这里使用模式匹配的位置以及它在这里是如何工作的。 我的问题是,我只看到了防护装置和一种将所有东西与体重和身高联系在一起的图案。

线条

(skinny, normal, fat) = (18.5, 25.0, 30.0)
是一种模式绑定——模式是
(瘦、正常、胖)
,一种绑定三个名称的元组模式。您还可以在
where
(和
let
)中使用其他类型的模式,例如:

head' :: [a] -> a
head' list = x
    where
    x : xs = list

这里的
x:xs
是一个绑定两个名称的模式。当然,在这种情况下,这是没有必要的,我们可以把模式放在论点中。但它偶尔也会派上用场。

对不起,为什么这个问题被否决了?
head' :: [a] -> a
head' list = x
    where
    x : xs = list