[Haskell] Views in Haskell
Simon Marlow
simonmarhaskell at gmail.com
Wed Jan 24 10:51:53 EST 2007
Malcolm Wallace wrote:
> Simon Peyton-Jones <simonpj at microsoft.com> wrote:
>
>> I have finally written up a proposal for adding
>> "view patterns" to Haskell.
>> http://hackage.haskell.org/trac/haskell-prime/wiki/ViewPatterns
>
> I have taken the liberty of correcting a couple of mistakes in your
> example code (on the wiki).
>
> To add to the bikeshed discussion of syntax, did you consider and reject
> the obvious use of '<-' rather than '->', which would more closely match
> the pattern guard syntax? For example,
>
> f ((name,rest) <- regexp "[a-z]*") = ...
Yes, I'd prefer that too. However, note that while the right arrow syntax looks
confusing when used on the left of a case alternative:
case e of
(regexp "[a-z]*" -> (name,rest)) -> ...
the left arrow syntax looks strange when used in a statement:
do
((name,rest) <- regexp "[a-z]*") <- readFile "foo"
...
although on balance, "pattern on the left of <-" is a better rule than "pattern
on the left of -> when it is preceded by \, but on the right otherwise" :-)
Being able to distinguish binding from bound occurrences of variables quickly is
very important for reading code, I'm worried that there won't be a good syntax
for view patterns that gets this right.
Personally I remain to be convinced by view patterns. All these examples can be
rewritten using either auxiliary functions, pattern guards, monads, or a
combination of these. Too much choice is counter-productive.
> parsePacket ((n, (val,bs) <- bits n) <- bits 3) = ...
> vs
> parsePacket (bits 3 -> (n, (bits n -> val bs))) = ...
are these significantly better than
parsePacket p = parseBits p $ do n <- bits 3; val <- bits n; ...
given a simple bit-parsing monad?
(I just noticed that Duncan posted something similar while I was writing this,
oh well).
Cheers,
Simon
More information about the Haskell
mailing list