LukasWorld
Understanding Charlists
in iex:
| iex > [ 'cat' | 'dog' ]
|> ['cat',100,111,103]
Why does IEx print ’cat’ as a string, but ’dog’ as individual numbers?
Marked As Solved
hauleth
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]
3
Also Liked
kokolegorille
Hello,
There are some points to clarify in your code…
- ’ is not equal to ", the first define a charlist, the second is a binary
- ‘cat’ is a charlist, or a list of char if You prefer… and ‘dog’ is equal to [100, 111, 103]
- to get the value of a character, You can use ?, eg. ?a => 97
- 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 ![]()
I recommend this page to dig deeper
https://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html
1
Popular in Questions
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
Hey,
I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Other popular topics
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Hey :wave:t3: Elixir community,
I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New







