Save 45% for a limited time.

Get access to all courses and lessons on egghead today.


    Lists and Infix operators in PureScript

    Vincent OrrVincent Orr

    List in PureScript have many performance benefits over Arrays but have a unique syntax and can be confusing when getting started.

    Learn how to construct List’s in PureScript with cons and see what the : infix operator is all about.

    0.11.7 - ^0.12


    Become a Member to view code

    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


    Instructor: 00:00 We'll start off with lists. Let's show you the data type. It's data list A = nil pipe cons A (list A). We've created data constructor called list, and that takes an A. Then, I'll either return it to nil or a cons, which itself takes an A and the list of A.

    00:25 This might not make too much sense, let's create our first list. We'll call it myIntList, and it has a type of list int. On the next line, myIntList = (cons 1 nil). Let's log that out. We'll delete this and we'll put in myIntList. As you can see, it's doing one.

    00:48 Oh, colon. Why is there a colon? Now, time to explain infix operators. In PureScript, we tend to use infix operators to either convert a type or a function into some sort of symbol.

    01:00 In this case, we've got the type cons into a colon. You can still use cons, but this just becomes an alternative. Let's type out infix, and then a number, in this case eight. That's just how tightly it binds. And what you want to create an infix for, in our case, cons as {colon}.

    01:16 Let's add some more attributes to myIntList. We'll do cons 2, cons 3 and there, close those off. It should make a little bit more sense now.

    01:27 We've got cons 1, and that's its first value. Then its second value is the rest of the list. If you look at cons 3, its second value is nil. That's why the final cons will always need one value and a nil, because the compiler has to know when the end of the list occurs.

    01:41 To do that, we use nil. Looking at our log, you can see there's one colon 2, colon 3, colon nil. Which is our list. There you have it, an introduction to list and infix operators.