2014-11-21 78 views
0

我想實現一個函數比較2列表,看看他們是否是相同的。語法看起來好像沒什麼問題:Haskell:輸入語法錯誤(意外的'=')

compare :: String -> String -> Bool 

    compare [] [] = True -- error here 
    compare (x,xs) (y,ys) = if x == y 
     then compare xs ys 
     else False 

,但我不斷收到這個錯誤在該行上面標明:

Syntax error in input (unexpected `=')

當我試圖取代「=」「 - >」,它工作得很好,但它在以下行中給出了相同的錯誤。所以,我做了同樣的:

compare :: String -> String -> Bool 

     compare [] [] -> True 
     compare (x,xs) (y,ys) -> if x == y -- new error here 
      then compare xs ys 
      else False 

但我得到一個不同的錯誤:

Syntax error in type signature (unexpected keyword "if")

現在我真的不知道發生了什麼事情。

回答

4

您的原始第一個功能是正確的,只是您的模式匹配錯誤。它應該是這樣的:

compare (x:xs) (y:ys) -- Not compare (x,xs) (y,ys) 

同樣@ThreeFx建議,請格式化您的代碼正確。最終它應該看起來像這樣:

compare :: String -> String -> Bool 
compare [] [] = True 
compare (x:xs) (y:ys) = if x == y 
         then compare xs ys 
         else False 
+0

是的,你是對的。非常感謝你! – SalmaFG 2014-11-21 22:16:22