⚠️ This lesson is retired and might contain outdated information.

Lists and Infix operators in PureScript

Vincent Orr
InstructorVincent Orr
Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 2 years ago

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.

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.

egghead
egghead
~ 10 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today