Qqwy

Qqwy

TypeCheck Core Team

What can you do with improper lists?

A proper list is a list that ends in an empty list, such as:

[1, 2, 3] (which is syntactic sugar for [1 | [ 2 | [3 | [] ] ] ] )

An improper list is a list that does not end with an empty list. An example:

[1, 2 | 3] (which becomes [ 1 | [ 2 | 3 ] ] but cannot be rewritten further).

I’ve seen multiple resources (such as The wonderful Elixir in Action book and this question on StackOverflow) state that “many of the list-related functions do not work on them, but there are some cool things you can use them for.”

What cool things are hinted at? When are improper lists useful?

Most Liked

sasajuric

sasajuric

Author of Elixir In Action

I don’t recall ever using it myself. I’d say it’s rarely called for. One example that comes to mind is saving a bit of memory. A pair represented as an improper list is smaller than with tuple:

iex> :erts_debug.size([1|2])
2

iex> :erts_debug.size({1,2})
3

So consequently, if you need to construct a bunch of pairs, you might save 1 word per pair. Note that for larger tuples (size >= 3) this won’t hold, and improper lists will in fact consume more memory.

Another example, which is mostly interesting in Erlang is simulating lazy lists (see here, the “Infinite Lists” example).

These are the only cases I can think of, so I’d say the usage is quite limited. I’d probably consider improper list only to save memory in some special cases, but I’d need very good reasons to go down that road. Since improper list could obscure the code, by default, I’d go for plain tuples.

JEG2

JEG2

Author of Designing Elixir Systems with OTP

Here’s a link to where the discussion turns to improper lists.

NobbZ

NobbZ

Deeply burried in Weird SASL lists and can I rely on Kernel.is_list? - #12 by JEG2 there is some of the cool stuff you can do mentioned and explained.

Since I’m on mobile right now I have trouble to do better linking than the hole thread…

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
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
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Other popular topics Top

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
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

We're in Beta

About us Mission Statement