Choking on Syntactic Sugar

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

Choking on Syntactic Sugar

Maurice Castro
Hi All,
        When writing a parser for Erlang we discovered an interesting
construction in regexp.erl (line 121 in our somewhat older distribution).

reg4([$.|S]) -> {{comp_class,"\n"},S};
reg4("[^" ++ S0) ->
    case char_class(S0) of
        {Cc,[$]|S1]} -> {{comp_class,Cc},S1};
        {Cc,S} -> throw({error,{unterminated,"["}})
    end;

It appears as if the ++ append operator has developed a new trick, it
can be used to divide a string into 2 parts (just like Prolog).

My question is in 3 parts:

        1) is this behaviour desirable
                - note it is pure syntactic sugar for [$[, $^ | S0]
        2) is this behaviour intended
        3) is it documented

Comments welcome.

        Maurice Castro


Reply | Threaded
Open this post in threaded view
|

Choking on Syntactic Sugar

Claes Wikström

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]

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

Raimo Niskanen-3
In reply to this post by Maurice Castro
Maurice Castro wrote:

>
> Hi All,
>         When writing a parser for Erlang we discovered an interesting
> construction in regexp.erl (line 121 in our somewhat older distribution).
>
> reg4([$.|S]) -> {{comp_class,"\n"},S};
> reg4("[^" ++ S0) ->
>     case char_class(S0) of
>         {Cc,[$]|S1]} -> {{comp_class,Cc},S1};
>         {Cc,S} -> throw({error,{unterminated,"["}})
>     end;
>
> It appears as if the ++ append operator has developed a new trick, it
> can be used to divide a string into 2 parts (just like Prolog).
>
> My question is in 3 parts:
>
>         1) is this behaviour desirable
>                 - note it is pure syntactic sugar for [$[, $^ | S0]
>         2) is this behaviour intended
>         3) is it documented

Check out
        http://www.erlang.se/doc/doc-5.0.2/doc,
        Link "Erlang Extensions Since 4.4",
        Section 7.8 "Literal string prefix in patterns"

/ Raimo Niskanen, Ericsson UAB, Erlang/OTP


Reply | Threaded
Open this post in threaded view
|

Choking on Syntactic Sugar

Pekka Hedqvist
In reply to this post by Maurice Castro
It is documented:
http://www.erlang.org/doc/r7b/doc/extensions/part_frame.html

>-----Original Message-----
>From: owner-erlang-questions
>[mailto:owner-erlang-questions]On Behalf Of Maurice Castro
>Sent: Tuesday, March 13, 2001 8:10 AM
>To: erlang-questions
>Subject: Choking on Syntactic Sugar
>
>
>Hi All,
> When writing a parser for Erlang we discovered an interesting
>construction in regexp.erl (line 121 in our somewhat older distribution).
>
>reg4([$.|S]) -> {{comp_class,"\n"},S};
>reg4("[^" ++ S0) ->
>    case char_class(S0) of
>        {Cc,[$]|S1]} -> {{comp_class,Cc},S1};
>        {Cc,S} -> throw({error,{unterminated,"["}})
>    end;
>
>It appears as if the ++ append operator has developed a new trick, it
>can be used to divide a string into 2 parts (just like Prolog).
>
>My question is in 3 parts:
>
> 1) is this behaviour desirable
> - note it is pure syntactic sugar for [$[, $^ | S0]
> 2) is this behaviour intended
> 3) is it documented
>
>Comments welcome.
>
> Maurice Castro
>



Reply | Threaded
Open this post in threaded view
|

Choking on Syntactic Sugar

Robert Virding-4
In reply to this post by Claes Wikström
Klacke <klacke> writes:
>
>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.

-- is a joke.  I had planned to do something with ** and // as well but never
got around to it.  Any suggestions? :-)

        Robert




Reply | Threaded
Open this post in threaded view
|

Choking on Syntactic Sugar

Chris Pressey
Robert Virding wrote:
> Klacke <klacke> writes:
> >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.
> -- is a joke.

I did laugh the first time I saw it, but I've since found it
unexpectedly convenient!  :-)

> I had planned to do something with ** and // as well but never
> got around to it.  Any suggestions? :-)

** could do lists:duplicate/2 (perhaps with argument order reversed)
akin to Perl's 'x' operator.  e.g: io:fwrite("~s", ["-" ** 72]).

// could do lists:nth/2 (again, perhaps with reversed arguments)
somewhat akin to array-indexing other languages.  e.g: [foo,bar,baz] //
2.

It might be nice if all of these (++, --, **, //) worked the same on
tuples.

Also, it would be (IMO) very cool to make <- meaningful outside of a
list-comprehension (in a guard and/or expression).  X <- [1,2,3] would
evaluate to true if X == 1 or X == 2 or X == 3.  It would also be great
if this could be expanded at compile-time for constant lists (although I
realize that might get a little hairy.)

_chris

--
"Ten short days ago all I could look forward to was a dead-end job as a
engineer.  Now I have a promising future and make really big Zorkmids."
Chris Pressey, Cat's Eye Technologies, http://www.catseye.mb.ca/
Esoteric Topics Mailing List: http://www.catseye.mb.ca/list.html