[erlang-questions] Strings as Lists

Claes Wikstrom klacke@REDACTED
Sat Mar 1 21:10:33 CET 2008

Richard A.O'Keefe wrote:

> so it's easy for an Erlang program to build a string cheaply in  
> either left
> to right or right to left order using lists, but wouldn't be using  
> binaries.

iolists are great for cheaply constructing data that is about to
go out on port soon. iolists suck when some code later on has to
traverse/manipulate it. The ports will just automatically flatten the iolist
in  O(n) and that (n) is negligible because the port code anyway has
to copy the data to an output buffer which is _also_ O(n)

One of the original goals with the binary() datatype was that
appending to the right should be as cheap as it is for an iolist
and it is.

List2 = [List1 | More]


List2 = [List1 , More]

doesn't really matter as long as More indeed is a list


Bin2 = <<Bin1/binary, More/binary>>

is equally efficent as the iolist construction. It's just
pointer manipulations. O(1)


More information about the erlang-questions mailing list