[Haskell-beginners] case as guards in lambda functions

Russ Abbott russ.abbott at gmail.com
Wed Nov 10 15:13:26 EST 2010

I have often wanted to use a guard in a lambda function and had thought it
wasn't possible. But apparently the case construct allows a viable approach.
Here is a silly example.

testCase = map
             (\xs -> case xs of
                     []              -> "empty list"
                     [y] | y < 5     -> "small singleton"
                         | otherwise -> "large singleton"
                     _               -> "multi-element list")

> testCase [[], [2], [7], [1,2,3]]
["empty","small singleton","large singleton","multi-element list"]

It seems particularly useful to be able to include both patterns and guards
in case expressions. I haven't seen this usage anywhere. Is it considered
bad form?
-- Russ *
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20101110/575b94b3/attachment.html

More information about the Beginners mailing list