Choking on Syntactic Sugar

Richard Carlsson <>
Tue Mar 13 19:29:49 CET 2001


On Tue, 13 Mar 2001, Hakan Stenholm wrote:

> I'm a littel confused here so I would be happy if anyone would care
> to explain.
> 
> After reading the documentation I get the impression that ++ is
> simply a short form of lists:append/2. I guess that this is not the
> case consdering that only arithmetic expressions that evaluate to
> constants are allowed in a patterns. So what exactly is a ++:
> 
> "[^" ++ S0  is the same as  [$[, $^ | S0]

>From the documentation on Erlang Extensions since 4.4:

  7.8 Literal string prefix in patterns
  
       This extension was added in Erlang 4.8 (OTP R5A). 
  
       A new construction is allowed in patterns, namely a literal
       string as the first operand of the ++ operator. Example: 

             f("prefix" ++ L) -> ...

       This is syntactic sugar for the equivalent, but harder to read

             f([$p,$r,$e,$f,$i,$x | L]) -> ...


No other non-constant uses of `++' are allowed.


> but what would happen in
> 
> fun(List1 ++ List2) -> ...
> 
> is it legal ? we only know what List1 is at run time so there
> shouldn't be any way to expand it to some constant pattern as in
> reg4("[^" ++ S0) ?

Exactly. That's why it's not legal.

	/Richard


Richard Carlsson ()   (This space intentionally left blank.)
E-mail: 	WWW: http://www.csd.uu.se/~richardc/





More information about the erlang-questions mailing list