4 Dec 20:39
Nested guards?
From: Iavor Diatchki <iavor.diatchki <at> gmail.com>
Subject: Nested guards?
Newsgroups: gmane.comp.lang.haskell.general
Date: 2007-12-04 19:41:37 GMT
Subject: Nested guards?
Newsgroups: gmane.comp.lang.haskell.general
Date: 2007-12-04 19:41:37 GMT
Hi,
Lately I have been using pattern guards more than usual and I find
that occasionally I need to nest them (i.e., I have alternatives with
a common prefix). This seems to happen when I need to do some
preliminary checking, followed by some decision making. Here is an
example:
server text
| Just xs <- parse text
, | "field1" `elem` xs = ... do one thing ...
| "field2" `elem` xs = ... do something else ...
server _ = ... invalid request ...
As far as I can see this should be a fairly simple change to the
pattern bindings extension. Would anyone else find this a useful
feature, and if so what syntax should we use?
-Iavor

buckets :: Indexable key => (a -> (key, rest)) -> [a] -> Map key [rest]
buckets f = Map.fromListWith (++) . map (\ a -> let (k,r) = f a in (k, [r]))
Btw. since I often need fromListWith with Maps of list types, I wonder
RSS Feed