haskell
2014-11-02 23 views 1 likes 
1

你好,我試圖用鱗片狀的條件,但我得到解析錯誤:哈斯克爾多個否則

parse error on input ‘|’ 

isAssignMent::String->Bool 
isAssignMent a 
    | a == "" = False 
    | otherwise 
     | (head trimmed) == '=' = True 
     | otherwise = False 
     where 
      trimmed = trimRightSide a [' ', '\n'] 

我在做什麼錯?謝謝

+0

'otherwise'只是爲TRUE; – Squidly 2014-11-05 14:47:01

回答

5

這是你想要的嗎?

isAssignMent::String->Bool 
isAssignMent a 
    | a == "" = False 
    | (head trimmed) == '=' = True 
    | otherwise = False 
     where 
      trimmed = trimRightSide a [' ', '\n'] 

Guard條款按順序檢查。您最終只需要otherwise條款。

+0

的代名詞這解決了這個問題,謝謝 – yonutix 2014-11-02 23:14:07

5

您還可以更地道與模式匹配這樣寫:

isAssignMent::String->Bool 
isAssignMent ""   = False 
isAssignMent a 
    | '=':_ <- trimmed = True 
    | otherwise   = False 
    where 
     trimmed = trimRightSide a [' ', '\n'] 
相關問題