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

Han*_*Sun 0 haskell ghci

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

bmiTell :: => Double -> String                                                  
bmiTell bmi                                                                     
 | bmi <= 1 = "small"                                                           
 | bmi <= 10 = "medium"                                                         
 | bmi <= 100 = "large"                                                         
 | otherwise = "huge"     
Run Code Online (Sandbox Code Playgroud)

当我加载此文件时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>
Run Code Online (Sandbox Code Playgroud)

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

bmiTell :: Double -> String                                                     
bmiTell bmi                                                                     
 | bmi <= 1 = "small"                                                           
 | bmi <= 10 = "medium"                                                         
 | bmi <= 100 = "large"                                                         
 | otherwise "huge" 
Run Code Online (Sandbox Code Playgroud)

错误信息:

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.
Run Code Online (Sandbox Code Playgroud)

有没有人有这个想法?

Sib*_*ibi 6

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

bmiTell ::  Double -> String  -- Notice that there is no =>
Run Code Online (Sandbox Code Playgroud)

在第二种情况下,您=在最后一行中丢失了.它应该是这样的:

| otherwise = "huge"  -- Notice the presence of =
Run Code Online (Sandbox Code Playgroud)

所以正确的工作代码将如下所示:

bmiTell ::  Double -> String
bmiTell bmi
  | bmi <= 1 = "small"
  | bmi <= 10 = "medium"
  | bmi <= 100 = "large"
  | otherwise = "huge"
Run Code Online (Sandbox Code Playgroud)