in iex:
| iex > [ 'cat' | 'dog' ]
|> ['cat',100,111,103]
Why does IEx print ’cat’ as a string, but ’dog’ as individual numbers?
in iex:
| iex > [ 'cat' | 'dog' ]
|> ['cat',100,111,103]
Why does IEx print ’cat’ as a string, but ’dog’ as individual numbers?
Hello,
There are some points to clarify in your code…
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
I recommend this page to dig deeper
https://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html
Because what you have created is list with list as it’s head, this isn’t the same as 'catdog'
.
Lets see:
full = 'catdog'
joined = [ 'cat' | 'dog' ]
assert hd(full) == 97
assert hd(joined) == 'cat'
inspect full, charlists: :as_lists
# => [99, 97, 116]
inspect joined, charlists: :as_lists
# => [[99, 97, 116], 100, 111, 103]