Why are charlist and string co-existing in Erlang?

It’s just an interest question; why are charlist and string, which have similar roles, co-existing in Erlang?

Is there a historical reason?

Perhaps there was no string in ancient Erlang?

If so, from which version did the string come out?

3 Likes

Strings have always been charlists in Erlang from the very beginning. They still are actually though some libraries have been extended to allow binary strings. This was quite common in functional languages way back when to use lists of characters as strings.

If you want to be really strict neither Erlang nor Elixir actually has a real string datatype. Erlang as default uses lists of integers which it interprets as unicode points while Elixir uses binaries which it interprets as utf-8 encoded strings. So:

iex(1)> <<97,98,99>>
"abc"
iex(2)> <<194, 163, 194, 162, 194, 167>>
"£¢§"
iex(3)> <<194, 163, 194, 162, 194, 167>> == "£¢§"
true
iex(4)> is_bitstring("£¢§")
true

There isn’t even a character datatype, they are just integers.

13 Likes