为什么这个helloworld haskell片段无法加载?

时间:2015-05-09 07:49:19

标签: haskell ghci

我用以下代码编写名为“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.

有没有人有这方面的想法?

1 个答案:

答案 0 :(得分:6)

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

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"