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

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


<aside>
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.

[1] http://haskell.org/pipermail/haskell-cafe/2003-July/004708.html

[2] ftp://ftp.research.bell-labs.com/dist/smlnj/papers/92-tr-aitken.ps
</aside>

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

#g


------------
Graham Klyne
For email:
http://www.ninebynine.org/#Contact



More information about the Haskell-Cafe mailing list