[Haskell] Re: Nested guards?

Christian Maeder Christian.Maeder at dfki.de
Wed Dec 5 05:40:48 EST 2007


Iavor Diatchki wrote:
> Hi,
> Lately I have been using pattern guards more than usual and I find
> that occasionally I need to nest them (i.e., I have alternatives with
> a common prefix).  This seems to happen when I need to do some
> preliminary checking, followed by some decision making.  Here is an
> example:
> 
> server text
>    | Just xs <- parse text
>    ,   | "field1" `elem` xs   = ... do one thing ...
>        | "field2" `elem` xs   = ... do something else ...
> 
> server  _ = ... invalid request ...

Not having used pattern guards so far, can't you write?

server text = case parse text of
  Just xs
    | "field1" `elem` xs  -> ... do one thing ...
    | "field2" `elem` xs  -> ... do something else ...
  _ -> ... invalid request ...

Christian


More information about the Haskell mailing list