Charlists

A charlist is the string of Erlang: a list whose items are code points. ~c"hi" is [104, 105], and the two values are the same list. Elixir uses UTF-8 binaries for its own text and keeps the charlist for the functions that were written before it existed, so a program meets one at the boundary with :erlang, :string and :filename.

A list of code points

The ~c sigil writes a charlist, and the inspector prints it back in the same form. It is a list, so is_list/1 answers true, is_binary/1 answers false, and the head is the first code point:

literal.exs

chars = ~c"hi" IO.inspect(chars) IO.inspect(is_list(chars)) IO.inspect(is_binary(chars)) IO.inspect(hd(chars)) IO.puts(length(chars))

elixir literal.exs

~c"hi"
true
false
104
2

The length is two because a charlist has one item per code point, and the head prints as the integer 104 because that is what the item is. Before Elixir 1.15 a charlist was written with single quotes, 'hi'; that spelling is deprecated and the warning asks for ~c"hi", which says what the value is.

to_string and to_charlist

The two conversions are in the kernel: to_string/1 turns a charlist into a binary and to_charlist/1 goes the other way. List.to_string/1 is the same call with the list in its name, and String.to_charlist/1 keeps the UTF-8 code points of a text that is not ASCII:

convert.exs

IO.inspect(to_string(~c"hi")) IO.inspect(to_charlist("hi")) IO.inspect(List.to_string([104, 105])) IO.inspect(String.to_charlist("héllo"))

elixir convert.exs

"hi"
~c"hi"
"hi"
[104, 233, 108, 108, 111]

The accented é becomes the single code point 233 in the charlist, because a charlist counts characters rather than the bytes of their UTF-8 form. A code point above 255 is still one item, so a charlist is not a byte string and the two must not be confused.

The Erlang functions that want one

The modules of OTP write their answers as charlists when their input was one. :erlang.binary_to_list/1 converts a binary to a list of bytes, :string.uppercase/1 cases a charlist, and :filename.join/1 returns the path in the form it was given:

erlang.exs

IO.inspect(:erlang.binary_to_list("abc")) IO.inspect(:string.uppercase(~c"abc")) IO.inspect(:filename.join([~c"/tmp", ~c"notes.txt"]))

elixir erlang.exs

~c"abc"
~c"ABC"
~c"/tmp/notes.txt"

The :string module is the charlist counterpart of String: it counts characters, ignores case and trims, all on lists. A modern program calls the Elixir module and converts at the call that needs the other form, rather than carrying a charlist through its own code.

What a charlist meets in the standard library

A charlist is enumerable like any list, and IO.puts/1 prints it as text, but the String module wants a binary and refuses a list. The boundary is therefore one conversion wide:

stdlib.exs

IO.puts(~c"printed as text") IO.inspect(Enum.map(~c"abc", & &1)) IO.inspect(Enum.map(~c"abc", &<<&1>>)) IO.inspect(String.length(to_string(~c"héllo")))

elixir stdlib.exs

printed as text
~c"abc"
["a", "b", "c"]
5

The first map answers the code points and the inspector shows them as a charlist; the second turns each one back into a one-byte binary. String.length(~c"hi") would raise a FunctionClauseError, because every clause of the function asks for a binary. The rule is short: a charlist that arrives from an Erlang function is converted at once, and the program works on binaries.

The mistake: a charlist joined with a binary

The double plus concatenates lists, and a binary is not a list. The compiler accepts the line, the run builds an improper list whose tail is the binary, and the inspector marks the tail with a vertical bar:

mistake.exs

chars = ~c"abc" joined = chars ++ "def" IO.inspect(joined) IO.puts(length(joined))

elixir mistake.exs

[97, 98, 99 | "def"]
** (ArgumentError) errors were found at the given arguments:

  * 1st argument: not a list

    :erlang.length([97, 98, 99 | "def"])
    mistake.exs:5: (file)

The value [97, 98, 99 | "def"] is not a proper list, so length/1, Enum and almost every other function refuse it. The fix is to convert one side first: to_string(chars) <> "def" for a binary, or chars ++ to_charlist("def") for a charlist. The two forms look alike and produce different types, which is why the boundary is worth a conversion of its own.

The programs on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is a charlist?

    Answer

    A list whose items are code points

    The Erlang string is a list of code points, and Elixir writes it with the ~c sigil.

  2. Which call turns a charlist into a binary?

    Answer

    to_string/1

    to_string/1 converts the code points to their UTF-8 bytes; to_charlist goes the other way.

  3. Why does String.upcase(~c"abc") raise a FunctionClauseError?

    Answer

    Because every clause of the String function wants a binary

    The String module works on binaries, so a charlist is converted with to_string first.

  4. What does ~c"hi" print as?

    Answer

    ~c"hi"

    The inspector prints a charlist in the sigil form, which says what the value is.

  5. What does this program print? IO.inspect(hd(~c"hi"))

    Answer

    104

    The head of a charlist is the integer code point of the first character.

  6. What does this program print? IO.inspect(to_string(~c"hi"))

    Answer

    "hi"

    The conversion makes a binary, and the inspector shows a binary as a string.

  7. What does this program print? IO.inspect(:erlang.binary_to_list("abc"))

    Answer

    ~c"abc"

    The Erlang call answers a list of code points, and the inspector prints its charlist form.

  8. What does this program print? IO.inspect(String.to_charlist("héllo"))

    Answer

    [104, 233, 108, 108, 111]

    The charlist holds code points, and é is the single code point 233.

  9. What does this program print? IO.inspect(~c"abc" ++ "def")

    Answer

    [97, 98, 99 | "def"]

    The double plus builds a list, and a binary as the right side becomes the improper tail.

  10. What does this program do? chars = ~c"abc" IO.puts(length(chars ++ "def"))

    Answer

    An ArgumentError, because the value is not a proper list

    The tail is a binary, so length/1 gets an improper list and refuses it.

Article author: Arthur Isaev