为什么这个 helloworld haskell 片段加载失败?

Why does this helloworld haskell snippet fails to load?

我用下面的代码写了一个名为 "baby.hs" 的文件

bmiTell :: => Double -> String                                                  
bmiTell bmi                                                                     
 | bmi <= 1 = "small"                                                           
 | bmi <= 10 = "medium"                                                         
 | bmi <= 100 = "large"                                                         
 | otherwise = "huge"     

当我在 GHCi 中加载此文件时,它会这样抱怨:

ghci>:l baby.hs
[1 of 1] Compiling Main             ( baby.hs, interpreted )

baby.hs:1:12: parse error on input ‘=>’
Failed, modules loaded: none.
ghci>

如果我删除 =>,它也不起作用:

bmiTell :: Double -> String                                                     
bmiTell bmi                                                                     
 | bmi <= 1 = "small"                                                           
 | bmi <= 10 = "medium"                                                         
 | bmi <= 100 = "large"                                                         
 | otherwise "huge" 

错误信息:

ghci>:l baby
[1 of 1] Compiling Main             ( baby.hs, interpreted )

baby.hs:7:1:
    parse error (possibly incorrect indentation or mismatched brackets)
Failed, modules loaded: none.

有人对此有想法吗?

在你的第一种情况下,你的类型签名是错误的。应该是这样的:

bmiTell ::  Double -> String  -- Notice that there is no =>

在你的第二种情况下,你在最后一行缺少 =。应该是这样的:

| otherwise = "huge"  -- Notice the presence of =

因此,正确的工作代码将如下所示:

bmiTell ::  Double -> String
bmiTell bmi
  | bmi <= 1 = "small"
  | bmi <= 10 = "medium"
  | bmi <= 100 = "large"
  | otherwise = "huge"