polovy

polovy

List append (++) does not return a list

I am going through some tutorials, and one of the tasks is to create a list of items by taking first two elements of the existing item and appending them to the end of said list. The problem I have is that for some numerical values in the list, I get a strange response returned.

The Cos:

defmodule Tutorial do 
  def mirror_row(row) do
    [first, second | _tail] = row
    row ++ [second, first]
  end
end

the results I get are:

iex> Maps.mirror_row([1, 2, 3])
[1, 2, 3, 2, 1]
iex> Maps.mirror_row([121, 12, 123])
~c"y\f{\fy"

The 1st result is as expected, but the 2nd I would expect [121, 12, 123, 12, 121].
What is actually happening here and why?
I chacked this for different values and it does not make sense to me.

Some other results:

iex> Maps.mirror_row([123, 122, 121])
~c"{zyz{"
iex> Maps.mirror_row([123, 22, 12])
[123, 22, 12, 22, 123]
iex> Maps.mirror_row([121, 122, 123])
~c"yz{zy"
iex> Maps.mirror_row([121, 12, 123])
~c"y\f{\fy"
iex> Maps.mirror_row([121, 12, 13])
~c"y\f\r\fy"
iex> Maps.mirror_row([121, 22, 13])
[121, 22, 13, 22, 121]

Thanks.

Most Liked

dimitarvp

dimitarvp

IEx.configure(inspect: [charlists: :as_lists])

Run this manually inside iex or put it in a file called .iex.exs at the root of your project and it will be ran automatically on iex startup in that directory.

LostKobrakai

LostKobrakai

What you’re seeing are charlists using the ~c sigil. They are the exact data you expected, but printed as text to allow interoperability with erlang, which uses charlists as their default string type: List — Elixir v1.20.2

JEG2

JEG2

Author of Designing Elixir Systems with OTP

The folks calling Erlang functions that return “strings” that wouldn’t be readable.

I provide a detailed breakdown of the reasoning behind this feature and how to alter the behavior in Livebookisms. Here’s an exert:

Last Post!

LostKobrakai

LostKobrakai

In my experience lists with integers in valid ascii range are much rarer that seeing charlists around code interacting with erlang. Also I consider the information lost for a string not being represented as a string larger than the information lost for a very specific subsets of lists, which would be represented as a charlist.

In the end I also don’t think changing the default would mean new people are no longer tripped up by charlists. It’ll just happen in different circumstances.

Where Next?

Popular in Questions 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
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement