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

<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

W liĆcie z pon, 10-11-2003, godz. 17:56, Graham Klyne pisze:
mapL2V = \s -> case s of s | matchT s -> Just True | matchF s -> Just False | otherwise -> Nothing
Why not this? mapL2V = \s -> if matchT s then Just True else if matchF s then Just False else Nothing
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
No. But if it's fully applied, 'if cond then a else b' seems to be as clear. -- __("< Marcin Kowalczyk \__/ qrczak@knm.org.pl ^^ http://qrnik.knm.org.pl/~qrczak/
participants (2)
-
Graham Klyne
-
Marcin 'Qrczak' Kowalczyk