Choking on Syntactic Sugar

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Choking on Syntactic Sugar

Hakan Stenholm
>Maurice,
>
>On Tue, Mar 13, 2001 at 06:09:57PM +1100, Maurice Castro wrote:
>> My question is in 3 parts:
>>
>>       1) is this behaviour desirable
>
>Yes,
>
>>               - note it is pure syntactic sugar for [$[, $^ | S0]

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]

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) ?

>Yes,
>
>>       2) is this behaviour intended
>
>Yes,
>
>>       3) is it documented
>
>Yes, in the 4.4 Extensions pdf document at www.erlang.org.
>
>>
>> Comments welcome.
>>
>
>
>Yeah, if you feel the ++ operator is weird, take a look at the
>-- operator which is also described in the same doc, it's really
>weird. Not useless though.
>
>--
>Claes Wikstrom                        -- Caps lock is nowhere and
>Alteon WebSystems                     -- everything is under control          
>http://www.bluetail.com/~klacke   



Reply | Threaded
Open this post in threaded view
|

Choking on Syntactic Sugar

Richard Carlsson-4

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 (richardc)   (This space intentionally left blank.)
E-mail: Richard.Carlsson WWW: http://www.csd.uu.se/~richardc/