dwyl / dwyl/learn-elixir

what are improper lists?

Open
#120 1 comment 2 reactions 1 assignee Claimed by @SimonLab View on GitHub
enhancement
Dominant language
Elixir
Stars
1.7k
Forks
112
PR merge metrics
No merged PRs in 30d

Description

While looking at the code of the [`all/1` function](https://github.com/elixir-ecto/ecto_sql/blob/5e78291dbc8c0c6d249d8ad7150c120a4a836c59/lib/ecto/adapters/postgres/connection.ex#L106-L124) in the Ecto.Adapters.Postgres.Connection module (see https://github.com/dwyl/alog/issues/38 for the "why") we can see the returned value is
```elixir
[select, from, join, where, group_by, having, window, combinations, order_by, limit, offset | lock]
```

Let's use the syntax `[1, 2, 3]` and `[1, 2 | 3]` to understand the difference between lists and improper lists.
We know that `[1, 2, 3]` in Elixir represents a list. The **cons** operator `|` allow us to write the list as `[1 | [2 | [3 | [] ] ] ]`. This means that a list is the "combination" of a **head** and a **tail**:

- head: 1, tail [2, 3]
- head 2, tail [3]
- head 3, tail [] (empty list)

**A "normal" list in Elixir has always contains an empty list tail**

If we now apply the cons operator to the list `[1, 2 | 3]`(head `1` and tail `[2 | 3]`) we can see that we can't get an empty list for tail and the last tail is `[2 | 3]`. This is called an improper list.

**An improper list is a list which doesn't contain an empty list for the last tail**

- [ ] Add a new section under [data-structures](https://github.com/dwyl/learn-elixir#data-structures) to explain improper list

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.