Elixir Char lists

Welcome to a tutorial on Elixir. Here you will learn about Char lists in Elixir.

A char list is simply a list of characters. Check out the example below to understand this.

IO.puts('Hello')
IO.puts(is_list('Hello'))

The output of the above code is:

Hello
true

Rather than containing bytes, a char list contains the code points of the characters between single quotes. Therefore, the double-quotes represent a string (i.e. a binary), and single-quotes represent a char list (i.e. a list).

Note: The IEx will generate only code points as output if any of the chars is outside the ASCII range.

Also, the Char lists are commonly used when interfacing with Erlang, typically on old libraries that do not accept binaries as arguments. We can convert a char list to a string and back by using the to_string(char_list) and to_char_list(string) functions. Check out the code below.

IO.puts(is_list(to_char_list("hełło")))
IO.puts(is_binary(to_string ('hełło')))

The output of the above code is shown below.

true
true

Note- that the functions to_string and to_char_list are polymorphic, that is to say, that they can take multiple types of input like atoms, and integers and convert them respectively to strings and char lists.