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

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New

We're in Beta

About us Mission Statement