[erlang-questions] "prefix" ++ Rest = Something

Richard A. O'Keefe ok@REDACTED
Thu Oct 26 04:47:28 CEST 2017



On 26/10/17 3:11 AM, Pierre Fenoll wrote:
>> So this can not be described as a pattern.
>
> I'm saying let's fix that.
>
> Prefix ++ Rest as a pattern does not need to be calling ++/2.
> Instead, I am suggesting to allow "partial matches": adding support for
> these patterns.
> What makes this hard? Why is this only sugar?

(1) Consider Prefix ++ Rest = List where List has N elements.
     If Prefix and List are both unbound, there are N+1 solutions.
     If List is bound, there is at most one solution.
     If Prefix is bound, there is at most one solution.
     If Prefix is a list of patterns some of which are unbound,
     but the length of Prefix is fixed, there is at most one solution.

> Why is a similar pattern allowed in binaries but not with lists? (the
> <<Prefix:PrefixSize/binary, Rest/binary>> pattern)

     Did you notice the :PrefixSize part?  That has to be known.
     Otherwise it would have the same multiple-solutions problem.

(2) "prefix" ++ Rest as a pattern _doesn't_ call ++/2.
     It is translated to [$p|[$r|[$e|[$f|[$i|[$x|Rest]]]]]]
     by the parser.  (As noted above, the _elements_ of the list
     could perfectly well be any pattern, but the *spine* of the
     list must be visibly present.)

It's rather like the way Haskell used to allow
     f 0 = 1
     f (n+1) = f n * (n+1)
but did not allow n+m as a pattern.


Just how much of a problem _is_ this anyway?



More information about the erlang-questions mailing list