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

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement