否则Haskell是多重的

时间:2014-11-02 22:57:43

标签: haskell

您好我试图使用叠加条件,但我得到解析错误:

parse error on input ‘|’

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

我做错了什么?谢谢

2 个答案:

答案 0 :(得分:5)

这是你想要的吗?

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

按顺序检查Guard子句。你最后只需要一个otherwise条款。

答案 1 :(得分:5)

您还可以使用模式匹配来更加惯用地编写:

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