
10 Nov
2010
10 Nov
'10
3:13 p.m.
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 *