[erlang-questions] Strings as Lists

Richard Carlsson <>
Tue Feb 12 18:00:30 CET 2008


tsuraan wrote:
> Why does erlang internally represent strings as lists?  In every 
> language I've used other than Java, a string is a sequence of octets, 
> just like Erlang's binary type.  I know that you can represent a string 
> efficiently by using <<"string">> rather than just "string", but why 
> doesn't erlang do this by default?  Is it just because pre-12B binary 
> handling wasn't as efficient as list handling, or is Erlang intended to 
> support UTF-32?

Strings as lists is simple and flexible (i.e., if you already have lists,
you don't need to add another data type). Functions that work on lists,
such as append, reverse, etc., can be used directly on strings; you
don't need to program in different styles if you're traversing a list
or a string; etc. Other languages that represent strings as lists include
Prolog (which was a big influence on Erlang) and Haskell. That said, in
larger systems it is better to represent string constants in a more
space-efficient way. Binaries let you do this in Erlang, but they were
a later addition to the language, and the syntax for constructing and
decomposing binaries came even later.

     /Richard





More information about the erlang-questions mailing list