polovy

polovy

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.

Showing Posts 1 to 10

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

polovy

polovy OP

I suspected that this may be something like that. Is there a way of ‘forcing’ a standard list view in the console?
I assume that this will make no difference for the program (ie. I can pass the list to another function and it will work).

LostKobrakai

LostKobrakai

The link I posted shows how to configure the inspect protocol that way. IEx.configure can be used to configure iex with custom inspect options. Indeed the application itself doesn’t care. This is just something relevant to printing the data for human consumption.

polovy

polovy OP

Thanks.

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.

sbuttgereit

sbuttgereit

As an aside, I completely forgot this was out here…

Which, naturally, includes this very issue. I’m guessing this post is not very visible and only appears in searches… which in a case like this is more likely to not appear in a search since you’d kinda got to know what the issue was in the first place to hit the right search terms for it.

It’s a good idea… but I wonder if there’s a way to call this out for special attention so that it isn’t forgettable for those that might add to it or discoverable for those that might need it.

arcanemachine

arcanemachine

Cool, TIL. But this just flips the confusion the other way around, no?

Erlang/OTP 25 [erts-13.2.2.7] [source] [64-bit] [smp:20:20] [ds:20:20:10] [async-threads:1] [jit:ns]

Interactive Elixir (1.15.7) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> [97, 98, 99]                                  
~c"abc"
iex(2)> ~c"abc"
~c"abc"
iex(3)> IEx.configure(inspect: [charlists: :as_lists])
:ok
iex(4)> [97, 98, 99]                                  
[97, 98, 99]
iex(5)> ~c"abc"                                       
[97, 98, 99]

I’m guessing the tradeoff is that one is less likely to find themself surprised when a charlist is represented as a list of integers, compared to the more-common case where a list of integers is rendered as a charlist, which is a very common surprise for beginners. I suppose that if you’re deliberately working with charlists, you’re probably deep enough into the Elixir rabbit-hole to understand what is happening behind the scenes.


To confirm my obvious suspicion, the behaviour can be reverted to the default with this line:

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

Thanks again for another excellent tip. I’m gonna dogfood this one for a bit…

dimitarvp

dimitarvp

Yes, I am guessing the same. I don’t like the status quo either but unless we’re willing to go contribute to OTP itself (or maybe only to Elixir? not sure actually) then it’s going to stay that way and we should make the crutches obvious. At least they restore things to a bit saner state (hopefully).

arcanemachine

arcanemachine

Hey, I’m in no position to complain while I’m standing on the shoulders of giants. But I have not encountered this tip before, and I think it is definitely worth knowing.

It’s funny too, because it’s one of those things that’s obvious in retrospect. I’ve used a line like that to temporarily change the charlist rendering style. I just never though to put it in my IEx config.

dimitarvp

dimitarvp

I agree. It’s not obvious. I’d even argue this config should be the default; I really don’t see who would derive an actual tangible value out of seeing some integer lists as charlists.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews