Function matching w/ "abc" ++ Rest

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

Function matching w/ "abc" ++ Rest

Dave Smith-2

A general Erlang theory/performance question...

If I define a series of functions like so:


f1("abc" ++ Rest) ->
  abc;
f1("def" ++ Rest) ->
  def;


Is that ++ operator creating a new list and appending to it for the match? Are there scary amounts of list comparisons and overhead happening here? There doesn't seem to be anything about this form in the Erlang docs, yet it works. What's going on?

Thanks![/list]
_________________________________________________________
Sent using Mail2Forum (http://m2f.sourceforge.net)


Reply | Threaded
Open this post in threaded view
|

Function matching w/ "abc" ++ Rest

Michał Ptaszek
Assuming tha code was supposed to read:

f1("abc" ++ Rest) ->
 abc;
f1("def" ++ Rest) ->
 def;

You would be better off writing:

f1([$a,$b,$c|_Rest]) -> abc;
f1([$d,$e,$f|_Rest]) -> def.

Pete.

On Fri, 2005-12-09 at 07:01 +0100, dizzyd wrote:

> A general Erlang theory/performance question...
>
> If I define a series of functions like so:
>
>
> f1("abc" ++ Rest) ->
>   abc;
> f1("def" ++ Rest) ->
>   def;
>
>
> Is that ++ operator creating a new list and appending to it for the match? Are there scary amounts of list comparisons and overhead happening here? There doesn't seem to be anything about this form in the Erlang docs, yet it works. What's going on?
>
> Thanks![/list]
> _________________________________________________________
> Sent using Mail2Forum (http://m2f.sourceforge.net)
>




Reply | Threaded
Open this post in threaded view
|

Function matching w/ "abc" ++ Rest

Christian S
Provide reasoning.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20051209/818749d7/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Function matching w/ "abc" ++ Rest

Björn Gustavsson-3
In reply to this post by Michał Ptaszek
Peter-Henry Mander <erlang> writes:

> Assuming tha code was supposed to read:
>
> f1("abc" ++ Rest) ->
>  abc;
> f1("def" ++ Rest) ->
>  def;
>
> You would be better off writing:
>
> f1([$a,$b,$c|_Rest]) -> abc;
> f1([$d,$e,$f|_Rest]) -> def.

Why?

The compiler generates exactly the same code for the two
ways of writing the function.

/Bjorn

--
Bj?rn Gustavsson, Erlang/OTP, Ericsson AB


Reply | Threaded
Open this post in threaded view
|

Function matching w/ "abc" ++ Rest

Raimo Niskanen-3
In reply to this post by Michał Ptaszek

        f1("abc"++Rest) -> abc;

is just syntactical sugar for

        f1([$a,$b,$c|Rest]) -> abc;

so, you might even want to write

        f1("abc"++_) -> abc;

Since "abc" is a compile-time constant, it can be
expanded without runtime overhead. And it becomes
a rather extensive match, or perhaps just what you
would expect - all characters in the string are
matched.

erlang (Peter-Henry Mander) writes:

> Assuming tha code was supposed to read:
>
> f1("abc" ++ Rest) ->
>  abc;
> f1("def" ++ Rest) ->
>  def;
>
> You would be better off writing:
>
> f1([$a,$b,$c|_Rest]) -> abc;
> f1([$d,$e,$f|_Rest]) -> def.
>
> Pete.
>
> On Fri, 2005-12-09 at 07:01 +0100, dizzyd wrote:
> > A general Erlang theory/performance question...
> >
> > If I define a series of functions like so:
> >
> >
> > f1&#40;"abc" ++ Rest&#41; ->
> > &nbsp; abc;
> > f1&#40;"def" ++ Rest&#41; ->
> > &nbsp; def;
> >
> >
> > Is that ++ operator creating a new list and appending to it for the match? Are there scary amounts of list comparisons and overhead happening here? There doesn't seem to be anything about this form in the Erlang docs, yet it works. What's going on?
> >
> > Thanks![/list]
> > _________________________________________________________
> > Sent using Mail2Forum (http://m2f.sourceforge.net)
> >
>
>

--

/ Raimo Niskanen, Erlang/OTP, Ericsson AB


Reply | Threaded
Open this post in threaded view
|

Function matching w/ "abc" ++ Rest

Mats Cronqvist (ÄL2/EAB)
In reply to this post by Michał Ptaszek
   from the reference manual;
http://erlang.se/doc/doc-5.4.10/doc/reference_manual/expressions.html#6

"When matching strings, the following is a valid pattern:
f("prefix" ++ Str) -> ...
This is syntactic sugar for the equivalent, but harder to read
f([$p,$r,$e,$f,$i,$x | Str]) -> ..."

   note that f("foo" ++ _) works, and is the right choice for this example.

   mats

Peter-Henry Mander wrote:

> Assuming tha code was supposed to read:
>
> f1("abc" ++ Rest) ->
>  abc;
> f1("def" ++ Rest) ->
>  def;
>
> You would be better off writing:
>
> f1([$a,$b,$c|_Rest]) -> abc;
> f1([$d,$e,$f|_Rest]) -> def.
>
> Pete.


Reply | Threaded
Open this post in threaded view
|

Function matching w/ "abc" ++ Rest

Michał Ptaszek
In reply to this post by Christian S
Hi Christian,

As Bjorn has already pointed out there's no technical advantage in
either notation, I therefore have no logical reason to provide.

Sorry.

On Fri, 2005-12-09 at 10:37 +0100, Christian S wrote:
> Provide reasoning.
>