Unlock This Lesson

Autoplay

Construct Lists with Elm’s List Type

Enrico Buonanno

List is a union type that is recursively defined with Cons.

Follow along with the online editor at http://elm-lang.org/examples/hello-html

Elm

You must be a Member to view code

Access all courses and lessons, track your progress, gain confidence and expertise.

Become a Member
and unlock code for this lesson

Transcript

00:00 Finally, let's look at another very important union type, and this time, it's a recursive union type. It's the list type. This can be a bit surprising, but indeed, in many functional languages, including Elm, list is actually a union type.

00:16 More or less, the definition looks like this. You have type list of A. A is the type of the contents of the list. This can be nil. This means that a list could be an empty list -- in which case, there is no data -- or it could be a cons.

00:33 Cons, which means construct, is the construction of a list from an element of type A -- so that's the head of the list, the first item in the list -- and, recursively, a list of As. This is the tail of the list, so all elements after the first. Because of this recursive definition, list can actually represent lists of any length.

00:58 If we use this definition, we could declare a list, l1, to be empty -- so, nil -- and we could declare a list, l2, that has just one element -- so that would be a cons of, let's say, the value 1 and an empty list, nil.

01:15 If we wanted a list with two elements, l3, then that will be a cons where the first value is, for example, 1, and the second value in turn will be a cons of, say, 2 and nil. And so on. You could go on and represent lists of arbitrary length.

01:35 Of course, when you're working with Elm, there is already a list type. There is no need to define your own. Because it's such a common type, there is some shorthand syntax. Rather than nil, you would just use the empty brackets, and rather than cons, you would use the :: .

01:50 Let me comment out this definition. If you wanted an empty list, you would use the empty brackets. In the case of the cons, because the : is a function whose name is just composed of symbols, it's actually used as a name-fix operator.

02:10 Rather than cons 1 nil, instead of nil, we have the brackets, and the cons wouldn't go here, but it would go in the middle. Likewise, l3 would be 1 cons another list made of 2 cons nil. This compiles, and it's perfectly valid Elm code.

02:36 Of course, if you wanted a list with two elements, then writing it as we've done here is a very long-winded way, so there is shorthand syntax. You could have l4, and you would just write it like this -- 1,2.

02:49 Now, let me prove to you that this is really the same thing. I want to say that l3 and l4 are actually the same thing. This expression will be a Boolean. Let me convert it to string, and then let me feed this to the text function to make an HTML out of it.

03:12 Now, if I run the program, it says true, meaning that indeed, l3 and l4 are the same. When you write 1,2, what it's really doing is creating a cons with 1 and a tail which in turn is a cons between 2 and an empty list.