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!

1 Answer 1

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 ;-)

2
  • I thought maybe L1 ++ L2 is somehow different, because I couldn't find a way to transform it to conses 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, 2011 at 22:13
  • T ++ "\r\n" can't be transformed to conses so can't be used in pattern match. Nov 29, 2011 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.