Follow

Whenever I’ve wondered about how something in Erlang works, I have never been disappointed in the answer. I almost always leave with the impression that the designers did the “right thing”. I suppose this is in contrast to Java, which does the pedantic thing, Perl, which does the kludgy thing, Ruby, which has two independent implementations of the wrong thing, and C, which doesn’t do anything.

  • Evan Miller

Still one of my favourite quotes ever.

Source: evanmiller.org/why-i-program-i

@newt I recommend reading this post, it brings up some interesting points. The second paragraph counting from the one I quoted is about strings:

Or take string concatenation. If you pop open the implementation of string concatenation in Perl, Ruby, or JavaScript, you are certain to find an if statement, a realloc, and a memcpy. That is, when you concatenate two strings, the first string is grown to make room for the second, and then the second is copied into the first. This approach has worked for decades and is the “obvious” thing to do. Erlang’s approach is non-obvious, and, I believe, correct. In the usual case, Erlang does not use a contiguous chunk of memory to represent a sequence of bytes. Instead, it something called an “I/O list” — a nested list of non-contiguous chunks of memory. The result is that concatenating two strings (I/O lists) takes O(1) time in Erlang, compared O(N) time in other languages. This is why template rendering in Ruby, Python, etc. is slow, but very fast in Erlang.

@Amikke you can have O(1) string concatenation in any language. Although that means you'll have O(N) indexing. But this isn't the problem with Erlang strings.

The problem is, string in Erlang are lists of integers. If these integers fall within a certain range, the list is treated as a string. Otherwise, it isn't. Which may lead to funny bugs at times. I had to fix a bug in a logging library once because it logged some IP addresses as characters, not as sequences of numbers.

@newt right, that is a problem, forgot about that one xD

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.