2015-05-09 50 views
0

我寫了一個名爲「baby.hs」用以下代碼爲什麼helloworld haskell片段無法加載?

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. 

有沒有人有這方面的想法?

+0

'這是行不通的either'你是什麼意思? – thefourtheye

+0

@thefourtheye添加了「錯誤信息」,它只是說'解析錯誤' –

+0

爲什麼你要在你的類型簽名中加入一個約束符號'=>'?沒有用。另一個問題是在'else'之後缺少'='。 – AJFarmar

回答

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"