9

I'm going through "Pragmatic Programming Erlang" where there is a function defined like this:

split("\r\n\r\n" ++ T, L) -> {reverse(L), T};
split([H|T], L) -> split(T, [H|L]);
split([], _) -> more.

What interests me is first match, namely "\r\n\r\n" ++ T - is there performance difference between such a pattern and similar one, that I came up with: [13,10,13,10|T]? Or are they equivalent?

I know it's very simple question and that I could (probably) check it myself, but if there is a difference, I'd like to know why that is the case.

Thanks!

cji
  • 6,635
  • 2
  • 20
  • 16

1 Answers1

8

"\r\n\r\n" ++ T is just syntax sugar for [13,10,13,10|T]. It should perform same. If not there is something wrong ;-)

Hynek -Pichi- Vychodil
  • 26,174
  • 5
  • 52
  • 73
  • I thought maybe `L1 ++ L2` is somehow different, because I couldn't find a way to transform it to `cons`es when used like that: `T ++ "\r\n"` - but thanks to your answer I thought again and I realized that it is possible, although somewhat lengthy. Thanks! – cji Nov 28 '11 at 22:13
  • `T ++ "\r\n"` can't be transformed to conses so can't be used in pattern match. – Hynek -Pichi- Vychodil Nov 29 '11 at 14:31