# Non-empty list vs (head, tail)

**URL:** <https://discourse.elm-lang.org/t/non-empty-list-vs-head-tail/1376>\
**Category:** Learn\
**Created:** [June 10, 2018, 2:01am UTC](https://discourse.elm-lang.org/t/non-empty-list-vs-head-tail/1376 "2018-06-10T02:01:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![charliek](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/charliek/32/3865_2.png) [@charliek](https://discourse.elm-lang.org/u/charliek)\
**Post date:** [June 10, 2018, 2:01am UTC](https://discourse.elm-lang.org/t/non-empty-list-vs-head-tail/1376/1 "2018-06-10T02:01:52Z")

</div>

I saw some chatter in slack the other day about two ways of representing a list that is guaranteed to never be empty. One way is to **use a module that encapsulates this invariant** , such as [non-empty-list](http://package.elm-lang.org/packages/mgold/elm-nonempty-list/latest). Another way is to **use a tuple** `(a, List a)` where the first element is the head and the second element in the tuple is the remainder of this list (which could be an empty list)

It just so happens I have a package published where I was using `non-empty-list` and I changed the underlying implementation to now use a tuple…

[Non-empty vs tuple diff](https://github.com/ckoster22/elm-genetic/commit/b269207f5116d96e52982830bbb0f9bf6e3284b8)

Pros:

- One less dependency and import statement
- Once the list is reconstructed we can use elm-lang/core’s List functionality
- Removed custom non-empty helper function

Cons:

- Reconstructing the list from head / tail is a bit annoying
- Argument deconstruction + “as” syntax is less readable to me (lines 148 and 198 in Genetic.elm)
- I speculate that `(a, List a)` isn’t beginner friendly

I’m conflicted on the tradeoffs here. Depending on the context and team any of those three cons can be further emphasized.

I’m curious if someone can **demonstrate in code** or **has past experiences with larger codebases / teams** to help determine if the balance tips one way or the other.

---

<div class="post-metadata">

**Author:** ![Viir](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/viir/32/4103_2.png) [@Viir](https://discourse.elm-lang.org/u/Viir)\
**Post date:** [June 10, 2018, 5:38am UTC](https://discourse.elm-lang.org/t/non-empty-list-vs-head-tail/1376/2 "2018-06-10T05:38:11Z")

</div>

> [@charliek](#):
>
> I speculate that `(a, List a)` isn’t beginner friendly

Where and why would a beginner need to see this? Is there a problem with using a type alias instead?

```auto
type alias NonemptyList a = (a, List a)

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [June 20, 2018, 5:46am UTC](https://discourse.elm-lang.org/t/non-empty-list-vs-head-tail/1376/3 "2018-06-20T05:46:04Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
