Bug in list concatenation

Is this a bug or am I missing something? Please let me know.

[] ++ [130] returns [130]
[] ++ [120] returns ‘x’

iex runs inspect on the results, and a list with only integers between 32 and 127 is identical to an Erlang charlist - so it prints like one (note the ' punctuation).

You can tell inspect to not do that:

IO.inspect([] ++ [120], charlists: :as_lists)

See also the documentation for charlists Inspect.Opts — Elixir v1.13.2

5 Likes

Hi, my bad, sorry for not able to clearly explain my problem.
input:
[“hello”, “world”]
output:
[79,43]

these are the 2 different approaches I have tried
1)
x = [“hello”, “world”]
x |> Enum.map( fn name →
case name do
“hello” → 79
“world” → 43
end
end)
which returns ‘O+’

  1.  x |> Enum.reduce([], fn name , acc ->
       acc = acc ++
         case name do
           "hello" -> [79]
           "world" -> [43]
         end
     end)
    

which also returns same ‘O+’

but I want the output [79, 43]

The charlist 'O+' and the list of integers [79, 43] are identical.

'O+' == [79, 43] evaluates to true.

IO.inspect('O+', charlists: :as_lists) will print [79, 43].

6 Likes
2 Likes