Edward Hasted
Edward Hasted

Reputation: 3433

Elixir: ^A in list output

I have tried various ways to generate a list like:

 n = 3
 nodelist_master = Enum.into 1..n, []

But when I try and output them with IO.puts "List: #{nodelist_master}" or inspect I get

 ^A^B^C

I was expecting

 [1,2,3]

Upvotes: 0

Views: 113

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

String interpolation you used ("#{list}") implicitly calls Kernel.to_string/1 on the argument.

iex|1 ▶ list = Enum.into 1..3, []
#⇒ [1, 2, 3]
iex|2 ▶ to_string(list)        
#⇒ <<1, 2, 3>>

Lists of integers are output as ASCII chars for values < 127:

iex|3 ▶ IO.puts to_string(list)
#⇒ ^A^B^C
:ok

The above are ASCII codes for 1, 2, and 3 respectively. For 65 (which is ASCII code for "A" char):

iex|3 ▶ IO.puts [65]
#⇒ A

IO.inspect implicitly calls Kernel.inspect/2 on the argument:

iex|4 ▶ inspect(list)
#⇒ "[1, 2, 3]"
iex|5 ▶ IO.inspect list, label: "List"
#⇒ List: [1, 2, 3]
[1, 2, 3]

Upvotes: 4

Related Questions