Prefix string matching

Ulf Wiger <>
Fri Jul 28 21:36:02 CEST 2006


Den 2006-07-28 19:11:18 skrev andreas <>:

> I wonder if the following expression:
> add_prefix(String = "prefix" ++ _) ->
>    String;
> add_prefix(Path) ->
>    "prefix" ++ String.
>
> is  equal to:
>
>  add_prefix(String = [$p,$r,$e,$f,$i,$x|_]) ->    String;
>  add_prefix(Path) ->
>    "prefix" ++ String.

<aside>While these posts look very nice in the TrapExit forum,
they look really crummy in my Opera mail reader when they
come off the erlang-questions list. It would be much better
if the BBCode could be forwarded as-is, without escaping
parens etc.</aside>

Yes, the two are equivalent.

> Can the compiler optimize:
>  "Const" ++ VariableString
> to:
>  [$C, $o, $n, $s, $t | VariableString]

Well, the former is syntactic sugar for the latter.
I don't know if that counts as "compiler optimization"...


> If the compiler can not optimize this, how costly is
> ++ operations in pattern matching?

It is allowed only as syntactic sugar in cases like the
one above.

BR,
Ulf W
-- 
Ulf Wiger



More information about the erlang-questions mailing list