I am currently learning Elixir using Dave Thomas’s book Programming Elixir in which he states that
If all the values in a list represent printable characters, it displays the list as a string; otherwise it displays a list of integers
and am curious about the following:
a = 'r'
[head | _tail] = a
IO.puts head #This prints the binary representation of r: 114
IO.puts [head] #This print the letter r
Why does wrapping the head variable in a list change the way that it is displayed. If I instead do not split ‘a’ into a head and tail and instead print it out directly, it prints out a string always.
So 'hello'
and [104, 101, 108, 108, 111]
is the same thing. The last one being the internal format.
As to why it changes the display. IO.puts will interpret a charlist as string and print it as such. If the incoming parameter is not a list it will perform a to_string conversion which is different depending on the data type. In the case of [114] this can be displayed as r whereas 114 is just an integer which it will print as an integer.