[Haskell] Views in Haskell
Claus Reinke
claus.reinke at talk21.com
Wed Jan 24 08:20:20 EST 2007
> -- abstract list deconstructors / list pattern constructors
> -- (consP takes h/t sub-patterns as parameters)
> consP h t l = do { guard $ not (null l); hr <- h (head l); tr <- t (tail l); return (hr,tr) }
> nilP l = do { guard $ null l; return () }
>
> -- wildcard and variable patterns
> wildP l = return ()
> varP = return
>
> -- extract the head of the tail of the parameter list, if that list has two elements
> f (consP wildP (consP varP nilP) -> (_,(x,_))) = x
hmm, the above was probably guided too much by thinking about my own proposal
(and this style could be translated back to it fairly easily, I think). the following would
make better use of view patterns, and be a lot simpler:
-- cons pattern/deconstructor
consP l = do { guard $ not (null l); return (head l, tail l) }
-- extract head of tail of two-element list
f (consP -> (_, consP -> (x, []) ) ) = x
btw, lambda-match and view patterns complement each other:
- the sugar in lambda-match embeds classical matches in data parsing
- the sugar in view patterns embeds data parsing in classical patterns
In view of this, I was wondering: if you do not limit yourself to Maybe, but
allow other MonadPlus instances, wouldn't that give you or-patterns?
also, view patterns give us local guards:
g ( xs@( guard . not . null -> () ) ) ys = xs++ys
if we combine these two, we could presumably do things like using
the list MonadPlus for backtracking matches, as proposed in some other
functional languages (also assuming non-linearity of patterns here):
select :: Eq a => a -> Map a b -> b
select key ( toList -> ( (guard . (key==) ) ,value) ) = value
claus
More information about the Haskell-prime
mailing list