Is there an easier way to use guards in an expression...

Graham Klyne gk at
Mon Nov 10 16:56:50 EST 2003

This is almost a reprise of a question that came up back in July [1].  At 
the time, Mark Jones posted a reference [2], but I'm not seeing how it is 
relevant to this issue of case expressions.



The following code works, but it seems a bit clumsy:

mapXsdBoolean = DatatypeMap
     { -- mapL2V :: String -> Maybe Bool
       mapL2V = \s ->
         case s of
             s   | matchT s  -> Just True
                 | matchF s  -> Just False
                 | otherwise -> Nothing
       -- mapV2L :: Bool -> Maybe String
     , mapV2L = Just . (\b -> if b then "true" else "false")

The case expression seems rather an unwieldy way of getting guards into an 
expression.  If this was an ordinary variable declaration rather than a 
field definition, I'd use a function definition with guards, but I can't 
see how to make that work with a lambda expression.  Is there a neater way?

And a supplementary question... do the prelude or standard libraries define 
any function like this:

    cond :: Bool -> a -> a -> a
    cond True  a _ = a
    cond False _ b = b


Graham Klyne
For email:

More information about the Haskell-Cafe mailing list