So far we have only seen how to store a single value in a data structure. To store multiple values, we can use one of the following collections Elm provides:
Record. This section covers list. The rest will be covered in successive sections.
Creating a List
In Elm, a list is a data structure for storing multiple values of the same kind. List is one of the most used data structures in Elm. A list is created with square bracket literals. Each element in a list must be separated by a comma. Here are some examples:
Note: Elm style guide recommends using a space after
[ and a space before
], but when
elm repl prints a list, it doesn’t include those spaces because the style guide was created to improve the readability of code for humans.
What happens when we try to put values of different types in a list?
Elm doesn’t like that. We can also create a numeric list by specifying a range.
A range is created by specifying the first and last numbers in a sequence. It’s a nice shortcut that saves you from having to type out a long list of numbers. As of this writing, Elm can only use a range to create a list of numbers, but not other types such as character or string.
The Elm Platform also comes pre-loaded with the
List module. Like the
String module, it’s also automatically loaded by
elm repl. That’s why we don’t need to explicitly import it. The
List module contains many more functions for working with lists. Let’s go through some of them.
member function determines whether or not an item is present in a given list.
isEmpty function determines whether or not a list is empty, whereas the
length function returns the number of elements in a list.
Reversing a List
reverse function returns a new list that contains elements from the original list in reverse order.
List module provides multiple functions for putting lists together. Let’s start with something we’re already familiar with:
We can also combine more than two lists with
Just like the
List also provides the
append function for combining two lists.
++, combining more than two lists with
append is a bit tedious.
If we have a bunch of lists buried inside another list, we can use the
concat function to flatten it like this:
Finally, we can add an element to the front of a list using the
:: (pronounced cons) operator.
Splitting a List
Partitioning a List
Unlike strings, we can’t use a separator to split a list. What we can do is partition a list based on some criteria. The elements that satisfy the criteria will be put into one list and the ones that don’t into another. A predicate function is the perfect place to store our criteria. As a reminder, a predicate is a function that takes a value as an input and returns a boolean as an output. Here’s an example that partitions a list using an anonymous function as a predicate.
Here’s another example that uses a normal function as a predicate to partition a list.
Elements that satisfy the predicate are placed in the first list, the rest go into the second list. Notice how the partitioned lists are contained inside a tuple?
- A tuple is a container like a list, but we can put values of different types into it like this:
( 1, "Sobchak", [ 't', 'd' ] ). Tuples are quite useful for returning multiple results from a function. We will cover it in much more detail later in the Tuple section.
You may be wondering why the
partition function didn’t return a list instead of a tuple. After all a list can also hold multiple lists in it. That’s because lists must contain values of the same kind, but tuples don’t have to. In the future, if
partition decides to also return the number of elements in each list like this:
(3, [1,2,3], 3, [4,5,6]), it won’t be able to do that with a list. Tuples on the other hand are perfect for a situation like this.
Unzipping a List
Let’s say we have a list of tuples each containing two elements.
How can we split it into two lists? We can use the
unzip function to accomplish that.
The first list contains the first item from each tuple in the original list and the second list contains the second item. Notice that
unzip also returns a tuple instead of a list. You will see this pattern of functions returning multiple values in a tuple in most Elm code.
Sorting a List
Below is a list of top seven highest scores from regular season games in NBA history.
But they aren’t sorted in any particular order. How can we sort them in ascending order? We can do that by using the
sort function sorts a list only in ascending array. It doesn’t allow us to pass an argument specifying in what order we want the list to be sorted. What a let-down. If we want to sort a list in descending order, Elm makes us jump through a couple of hoops. Let’s try the next example in the
src/Playground.elm file as the code we’re about to type is a bit too long for the repl. Define a function called
descending right above
main to this:
elm reactor from the
beginning-elm directory in terminal if it’s not running already and go to
http://localhost:8000/src/Playground.elm in your browser. You should see the original list sorted in descending order:
Let’s go through the above code step-by-step. Not sure if you noticed, but in
main we used the
sortWith function instead of our stubborn friend
sort to sort the list of scores in descending order.
sortWith accepts two arguments:
- A comparison function
- A list that needs sorting
Given two values, a comparison function tells us whether the first value is equal, less than, or greater than the second value. As it happens, Elm provides a function called
compare which does just that. If the first value is less than the second,
LT. But if the first value is greater, it returns
GT. And if they both are equal, it returns
compare is defined in the Basics module. Elm puts generic values and functions that can operate on different types of data such as strings, lists, records, etc. into the
Basics module. Like
Basics module is also loaded automatically by the repl. That’s why we don’t need to explicitly import it.
We can also compare strings with the
Strings are compared based on alphabetical order. This is how words are ordered in an English dictionary. For example, the word “Thomas” is considered less than “Thompson” because the letter ‘a’ comes before the letter ‘p’ in the alphabet.
When we want to sort a list in descending order, we need the
compare function to behave in opposite manner. How can we make it do that? By creating another function that pulls a switcheroo on
compare like this:
compare actually meant
LT when it meant
GT. Now we can give this function to
sortWith and our list will get sorted in descending order.
The sorted list is then passed to the
Debug.toString function, which generates a string representation of the list. Finally, the
Html.text function renders the sorted list on a browser.
Converting a Value to a String
Earlier, in the Backward Function Application section, we used the
String.fromFloat function to convert a float to a string before rendering it on a browser with
Html.text like this:
In Elm, there are three ways to convert a value to a string:
String.fromInt— Converts an
String.fromFloat— Converts a
Debug.toString— Converts any kind of value to a string. This function is not meant to be used in production code. If you need to show a string representation of a value other than
Floatin production, you need to use localization. Similarly to the
Debugis also included in the Elm Platform and is automatically loaded.
- “Localization is the process of translating software user interfaces from one language to another and adapting it to suit a foreign culture.” - MDN
Note: Unfortunately, as of this writing Elm doesn’t provide an elegant approach to localization. Therefore, we won’t be covering it in this book.
sortWith actually opens a door for comparing values using any order we want, not just ascending or descending. Let’s say we want to sort certain characters from Game of Thrones based on how evil they are. Add the following function definition right above
Now let’s use
main to sort a list of evil characters.
Refresh the page at
http://localhost:8000/src/Playground.elm and you should see
sortWith expects from a comparing function is one of these values:
EQ. It doesn’t care how those values are computed. The
sort function is actually a specialized case of
sortWith. Internally, it just calls the
compare function directly to compare values. So if we write
sortWith like this, we get the behavior of
Filtering a List
List.filter also takes a predicate and a list of items. It then creates a new list containing all items from the original list that pass the test implemented by the predicate. Here are some examples:
isOdd function is a predicate that determines whether a given number is odd. We divide the number by
2 and check if the remainder is
0. If yes, it’s not an odd number. The
remainderBy function divides the second argument by the first and returns the remainder. It’s defined in the
isHost function is also a predicate that determines whether someone is a host from ever fascinating Westworld. It uses the
List.member function we used earlier to determine whether a given name is in a list of hosts.
Mapping a List
Programming is all about data transformation. We take an input and apply a sequence of functions to it until we arrive at a result that can be returned as an output. Let’s say we have a list of strings:
And we want to find out how many strings have lengths less than six. How can we accomplish that? Well, first we need to find out a length of each string in the list. How about we generate another list that contains just lengths, but not the strings themselves? We can do that easily with the
List.map applies the given function to each element in the original list and puts the result in a new list. Here we gave it
String.length which takes a string and returns its length. Next, we need to remove lengths that are greater than or equal to 6. We already know a function that knows how to do it —
And we have our answer.
Let’s work through one more problem to solidify our understanding of
List.map. Let’s say we want to find out whether any of the guardians have hyphen in their name. We can use
String.contains to check for a hyphen like this:
After a while, typing an anonymous function starts to get a little tiresome. What if we apply
map like this instead:
Whoa! That works too. Previously, we applied
String.contains with both arguments (
x) it needed to return a boolean. However, if we call it without the second argument (
x), we get back a partially applied function instead of a boolean value.
List.map then passes each string from the
guardians list, one at a time, to this partially applied function as the last argument. This results in a boolean value.
An anonymous function like
(\param -> someFunction x param) can always be rewritten as
(someFunction x) as long as
param is the last argument. Here’s one more example that checks if a guardian’s name starts with
The partial application technique also works with operators. Let’s rewrite one of our earlier examples that contained an operator using the partial application technique.
Notice that we had to flip the
< operator in order to achieve the same result. That’s because the partial application technique requires us to use operators in prefix style, which places an operator before the operands. If we hadn’t flipped the
< operator, we would have gotten a list of numbers that are greater than
Folding a List
We have created numerous lists with numbers in them, but we haven’t tried to add all the elements up yet. Let’s do that.
What we have done here is fold (or reduce) a list into a number that represents the sum of all elements in the list. The
foldl function takes three arguments: combining function, initial value, and a list. The combining function in turn takes two arguments: an element from the list and an accumulator. The following diagram illustrates various components of the
During the first application of the combining function, the initial value is passed to the accumulator as shown in the diagram below.
After the first application,
foldl repeatedly passes the accumulator (sum thus far) back to the combining function as the second argument until there are no more elements left in the original list. The figure below shows the step-by-step application of the combining function.
Since we are calculating a sum here, we used the
+ operator inside our combining function. But if we were calculating a product, our combining function would use the
* operator instead.
Why is it returning zero as the product? Oops… We forgot to change the initial value.
foldl passes the initial value (zero in our case) as the first argument to the combining function. The result of multiplying a number by zero is zero. If we change the initial value to
1, we get the expected result.
* operators are also functions. And all
foldl expects is a function as the first argument. It doesn’t care whether it’s an anonymous function or a normal function or an operator. Therefore, we can re-write the above examples in a much more succinct way like this:
In case of addition and multiplication, you can think of replacing the commas in the list with the
* operators respectively.
foldl is capable of reducing a list in many different ways, but if all you want to do is calculate sum or product, Elm already provides those functions as a convenience.
That was pretty anticlimactic, wasn’t it? I showed you in detail how to use
foldl to calculate the sum and product of a list only to find out later there already exist functions to do exactly that much more easily. To make it up to you, I’ll show you one more example that’s actually useful. Let’s say we want to calculate the total number of characters in this list:
We can use
foldl to easily reduce this entire list of strings to a single number.
Folding a List from Right
foldl folds a list from left as its name indicates. What that means is it begins its operation starting from the beginning of the list, but sometimes we need to fold a list starting from the end. The
List module provides another function called
foldr for that.
The structure of
foldr looks very similar to that of
For sum and product it doesn’t matter whether we start from the beginning or end of a list, but there are operators that produce different results depending on where we start. Let’s use one that we are already familiar with: power operator (
Earlier, we learned that
^ is right-associative whereas
* are left-associative. What that means is if we write an expression as shown below,
^ will start evaluating it from the right.
There are two reasons why
^ is right-associative in Elm:
^ is also right-associative in mathematics and Elm tries to follow the rules from mathematics as much as possible.
^ was left-associative, the end result could simply be computed by just multiplying the exponents. Let’s see some examples to understand what this means.
We applied parentheses to force
^ to evaluate from left. We were then able to simply multiply all the exponents on the right and use
^ only once to get the same result. Therefore, to make the
^ operator more meaningful, we need to evaluate an expression from the right. Now let’s see how
foldr behaves with
It can be hard to understand how
foldr works just by looking at the code above. The figure below illustrates what exactly happens when we apply
foldr also repeatedly passes the accumulator (result thus far) back to the combining function as the second argument until there are no more elements left in the original list. The figure below shows the step-by-step application of the combining function given to
What happens if we use
foldl? It starts applying the
^ operator from the left resulting in a different value which is not what we want.
- Here is how we arrived at the final result in the example above:
(4 ^ 1) = 4
(2 ^ 4) = 16
(3 ^ 16) = 43046721
Lastly, we can rewrite the above expressions more succinctly just by specifying the operator instead of the entire anonymous function as shown below.
Are they all evil?
Earlier we partitioned a list containing characters from Game of Thrones into two lists. The first list was infested with evil people whereas the second list was full of kind souls.
What if we want to find out if any one of the characters is evil? The
any function is designed to do just that.
There’s certainly evil lurking in that list. Just like the
any takes a predicate that tells whether a given character is evil or not. We can also find out if all characters are evil by using the
all function instead.
How can Hodor be evil, right? If you think about it,
all are also folding a list. They both reduce a list to a single boolean value. There are a few more of these functions that perform a special kind of fold. You can learn all about them here.
Take it or Drop it
Remember those annoying bouncers at famous night clubs who tend to let only good looking people in? If those bouncers were Elm programmers, they’d love the
drop function drops the specified number of elements from the beginning of a list and returns a new list with remaining elements. Once in a while, we get a nice bouncer who lets people on a first come first serve basis. They would definitely prefer
Watch out! The bouncer just let Freddy into the club.
take returns a new list containing the specified number of elements from the beginning of a list.
Head or Tail?
Conceptually speaking, a list is divided into two parts: head and tail. The first element is called the head and tail represents the rest of the elements.
List module provides functions for getting the head and tail of a list as shown below.
Elm cannot guarantee that it can return a value when asked for the head (or tail) of a list. If the list is empty there is no value to return. Therefore, it returns a
How List Works Behind the Scenes
List is sometimes also called a linked list as it is a linear collection of data elements, each pointing to the next element. An element in a list is called a node.
Even if there is no data in it, the last (empty) node does exist. If you don’t believe me check this out:
We started with an empty list and added
9 in-front of it using the cons (
::) operator. We then continued to add the rest of the elements to that list one at a time. When we create a list in this way, it starts to look like a recursive data structure meaning a list consists of nodes which themselves are lists. Because a node isn’t required to contain a value, an empty node is also considered a list.
List in Elm is actually defined as a recursive data structure. In the Recursive Types section, we will create our own implementation of a linked list that will work similarly to Elm’s implementation of
List to better understand how a recursive data structure works.
We covered quite a few functions from the
List module in this section, but there are still more included in that module. You can learn all about them here.