News for hackers. Stuff that matters.
Re:Why I Program in Erlang by liancheng on Tuesday October 30, 2012 @12:41AM (#4716103)
Man, are you serious when you said that "The result is that concatenating two strings takes O(1) time in Erlang, compared O(N) time in other languages"? Yes, you are right that Erlang represents strings with lists, and that's the VERY reason that Erlang must take O(N) time to concatenate two strings (N is the length of the first string).

Just check your /usr/lib/erlang/lib/stdlib-xxx/src/string.erl, and you'll find this:

  concat(S1, S2) -> S1 ++ S2.
and "++" is obviously a O(N) operator.

But yes, string concatenation is somewhat faster than other languages that represent strings with arrays of characters. Because in Erlang, it's O(N), while in others it's O(N+M), where N is the length of the first string and M is the length of the second.

Re:Why I Program in Erlang by defun on Tuesday October 30, 2012 @01:03AM (#4716173)
I guess what he was meaning is that in Erlang in most cases you can work with strings as iolists.

Maybe here you can find a bit better explanation: http://erlang.org/pipermail/erlang-questions/2012-May/066590...

Re:Why I Program in Erlang by lukego on Tuesday October 30, 2012 @04:20AM (#4716577)
Right. This is like the POSIX features writev() and 'struct iovec'. Essentially all of Erlang's binary output primitives are happy to take an iolist/iovec instead of a plain string and this means fewer concatenations and/or system calls. Good feature.
Re:Why I Program in Erlang by nivertech on Tuesday October 30, 2012 @08:00AM (#4717010)
iolists map nicely to gather-write writev(), but unfortunately there is no Erlang primitives for scatter-read readv()
Re:Why I Program in Erlang by jlouis on Tuesday October 30, 2012 @09:18AM (#4717223)
This isn't entirely true, see file:pread/2 for how to do it with a file. We lack a binary:pread/2 though :)
Re:Why I Program in Erlang by nivertech on Tuesday October 30, 2012 @10:33AM (#4717476)
thanks for pointing it out, looks like it will not work with sockets though.

Also spec says that it will return list of either strings or binaries, not exactly iolist.

Re:Why I Program in Erlang by masklinn on Tuesday October 30, 2012 @12:49PM (#4718191)
> Also spec says that it will return list of either strings or binaries, not exactly iolist.

It's a proper subset of iolists, since it reads from flat data (a file) there isn't much sense in its adding arbitrary amounts of nesting to it.

Re:Why I Program in Erlang by masklinn on Tuesday October 30, 2012 @04:49AM (#4716620)
He's talking about iolists (concatenating strings for IO, which is the most common use case and the one needed for e.g. templates), not straight strings.
Re:Why I Program in Erlang by klibertp on Tuesday October 30, 2012 @07:36AM (#4716958)
I wonder how many times that's need to be said until it sinks in? This is the third time and I didn't even go through half of comments yet...
Re:Why I Program in Erlang by jlouis on Tuesday October 30, 2012 @09:16AM (#4717216)
Many times :)

writev() is hard to grasp.

Re:Why I Program in Erlang by peerst on Saturday November 03, 2012 @05:57AM (#4736289)
Thats what not many realize, that iolists with binaries need never be pasted together. They are basically passed to the kernel with a writev