Function matching w/ "abc" ++ Rest

Mats Cronqvist mats.cronqvist@REDACTED
Fri Dec 9 10:48:56 CET 2005


   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.



More information about the erlang-questions mailing list