Palindrome

Palindrome

[7, 8, 9] in iex returns '\a\b\t' Why?

I just noticed this in Iex:

iex(1)> [1,2,3,4,5,6,7,8,9,10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
iex(2)> [7,8,9]
'\a\b\t'
iex(3)> [7]
'\a'
iex(4)> [6]
[6]
iex(5)> [6,7]
[6, 7]
iex(6)> [7,8]
'\a\b'

Why is this happening? :slight_smile:

Most Liked

kokolegorille

kokolegorille

Hello and welcome,

it’s because they are the same thing…

a charlist is a list of char.

iex> [97, 98, 99]
'abc'
iex> [97, 98, 99, 0]
[97, 98, 99, 0]
iex> [97, 98, 99] == 'abc'
true
iex> Enum.map 'abc', & IO.inspect(&1)
97
98
99
'abc'

If it can, it will output char :slight_smile:

Nicd

Nicd

For information how to change this behaviour, see this previous answer: IEX - char printing - odd behavior

hauleth

hauleth

As other said, in addition You can use i/1 helper function in IEx session to get more informations about given type:

iex(1)> i [7,8,9]
Term
  '\a\b\t'
Data type
  List
Description
  This is a list of integers that is printed as a sequence of characters
  delimited by single quotes because all the integers in it represent printable
  ASCII characters. Conventionally, a list of Unicode code points is known as a
  charlist and a list of ASCII characters is a subset of it.
Raw representation
  [7, 8, 9]
Reference modules
  List
Implemented protocols
  Collectable, Enumerable, IEx.Info, Inspect, List.Chars, String.Chars
NobbZ

NobbZ

Because that’s the way Erlang represents strings. It makes interaction a lot easier when errors from Erlang are prettyprinted as charlists by default rather than as a list of strings.

Nicd

Nicd

It’s not really treating them differently, because they are charlists. As charlists are just lists of integers that represent characters, there is no way to differentiate them from “normal” lists. IEx is the one treating them differently when outputting them, as it tries to be friendly to you.

There is a similar thing with binaries, where strings are just binaries that contain UTF-8 data. IEx will print those binaries as strings. If there is data that is not valid UTF-8 or is not printable, it will print the bytes as numbers.

Where Next?

Popular in Questions Top

lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
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
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement