4 Dec 22:55
Re: [Haskell] Nested guards?
Roberto Zunino <zunino <at> di.unipi.it>
2007-12-04 21:55:44 GMT
2007-12-04 21:55:44 GMT
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.
RSS Feed