Understanding Charlists

Hello,

There are some points to clarify in your code…

  1. ’ is not equal to ", the first define a charlist, the second is a binary
  2. ‘cat’ is a charlist, or a list of char if You prefer… and ‘dog’ is equal to [100, 111, 103]
  3. to get the value of a character, You can use ?, eg. ?a => 97
  4. a list is composed by a head and a tail, it is a linked list where head is an element, and tail is a list

What You really wanted is

iex> [?c, ?a, ?t | 'dog']     
'catdog'

# This is also equal to 
iex> [?c | [?a | [?t | 'dog']]]
'catdog'

# it's more common to use binary in Elixir

iex> "cat" <> "dog"
"catdog"

Don’t worry, we all have gone through this :slight_smile:

I recommend this page to dig deeper

https://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html

1 Like