[Haskell-cafe] Re: [Haskell] Nested guards?

Roberto Zunino zunino at di.unipi.it
Tue Dec 4 16:55:44 EST 2007


Neil Mitchell wrote:
>> server text
>>    | Just xs <- parse text = let
>>      x | "field1" `elem` xs   = error "... do one thing ..."
>>        | "field2" `elem` xs   = error "... do something else ..."
>>      in x
>> server  _ = error "... invalid request ..."
> 
> This now has the wrong semantics - before if parse text returned Just
> [] the error invalid request branch was invoked, now its a pattern
> match failure.

I missed that, thanks.

The MonadPlus way is not as elegant, but not too ugly I think:

server text =
  do   Just xs <- return $ parse text
       do   guard $ "field1" `elem` xs
            return "... do one thing ..."
         `mplus` do
            guard $ "field2" `elem` xs
            return "... do something else ..."
    `mplus`
       return "... invalid request ..."

Zun.


More information about the Haskell-Cafe mailing list