LukasWorld

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

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]

Also Liked

kokolegorille

kokolegorille

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

Where Next?

Popular in Questions Top

dotdotdotPaul
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Jim
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
polypush135
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
vertexbuffer
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
vac
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
Phillipp
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
ashish173
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

JDanielMartinez
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
yawaramin
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
openscript
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
axelson
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...
239 45766 226
New
fireproofsocks
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
fayddelight
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
magnetic
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

We're in Beta

About us Mission Statement