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

时间:2023-02-04 17:02:35

I write a file named "baby.hs" with the following codes

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

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

When I load this file in GHCi, it complains like this:

当我在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>

If I remove the =>, it doesn't work either:

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

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

Error info:

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.

Does anyone have ideas about this?

有没有人有这个想法?

1 个解决方案

#1


In your first case, your type signature is wrong. It should be like this:

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

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

In your second case, you are missing = in the last line. It should be like this:

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

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

So a proper working code will look like this:

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

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

#1


In your first case, your type signature is wrong. It should be like this:

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

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

In your second case, you are missing = in the last line. It should be like this:

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

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

So a proper working code will look like this:

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

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