[erlang-questions] Strings as Lists
Dmitrii 'Mamut' Dimandt
Fri Feb 15 09:51:56 CET 2008
Richard Carlsson wrote:
> 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.
This is only true for ASCII text ;) Non-ASCII gets screwed up badly:
lists:reverse("text") %% gives you "txet"
lists:reverse("?????") %% Russian for text becomes
[130,209,129,209,186,208,181,208,130,209] which is clearly not what I
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the erlang-questions