[Haskell-cafe] Prolog-style list syntax?

Viktor Dukhovni ietf-dane at dukhovni.org
Mon Jun 28 17:02:53 UTC 2021


On Mon, Jun 28, 2021 at 06:17:42PM +1200, Anthony Clayden wrote:

> One of the annoying (to me) inconsistencies with Haskell syntax is
> that `[]` means 'here comes a list'; but for the most common way to
> write a list, you can't use `[]`:
> 
> >    elem x []        = False
> >    elem x ( y : ys ) = ...

Yes, there's presently no way to pattern-match a list tail inside `[]`.

> Unfortunately ...
> 
> >    elem x [ y: ys ] = ...
> 
> Is valid syntax, where `:` is cons, so this is a nested list. I'm thinking
> that's relatively uncommon, and means the same as `[ ( y: ys ) ]`.
> 
> Would anyone be violently pro or anti changing the meaning of `:` appearing
> at top level in `[   ]` to mean cons-tail?

I'm on the violently against side on the question of `[x : ys]` being
reinterpreted as `(x : ys)`.  For any expression `e1, we have a
singleton `[e]`, and there should not be any surprising exceptions to
that rule.

If something new is to pattern match as `x : ys` inside `[]` it would
have to be invalid syntax presently.  So it could in principle be:

    [x,,ys]

but I'm concerned that this is too likely to be an inadvertent typo,
as in:

    [ x,
    , y ]

It is probably more useful to catch this sort of error, than to overload
the syntax for head + tail pattern matches.

So on the whole, I'm inclined to accept the status quo.  The annoyance,
if any, of having to use `(x : ys)` in pattern matches would not be
solved by adding a third syntax for lists.

-- 
    Viktor.


More information about the Haskell-Cafe mailing list