Answering first questions in PureScript

Today I started to read for the 3rd time Purescript by Example. First time with proper idea what I want to write :)

It should be helpful in future learning.

Following chapter 3 we can find that indentation matters. That’s a good finding!

And yes - we have at least one winner! In 3.13 there’s a nice explenation what does $ mean! It it connected with curring. It can be used to omit parentheses. It makes code more readible. Haskell people are using it all the time so probably it’ll be our second nature.

$ can be readed as: “evalutate on the right of it and apply result to function on the left side on it”.

Working example (after installing purescript-lists)

Take first element from generated list of integers from 1 to 20

head range 1 20 -- it will throw because PureScript interpreter will try to apply `range` to `head` and then 1 to result of it


head $ range 1 20 -- it will succeed because interpreter will first evaluate the range and next it'll take first element

Reading this chapter and looking at corresponding source code we cannot find other answer to yesterday’s questions (what does = do mean). Instead we can find some new language constructions:

  • :: - similar to Haskell’s type definition. On the left side we have name, on the right side - types
  • functions takes always one argument with automatic curring
  • >>> and <<< - composition operators. Bread and butter in functional world. One is “from left” and another is “from right”.

Composition

Let’s try consider following examples - we want all elements but first from filtered list of integers:

tail <<< filter (>10) $ 1..20 -- it will work

filter (>10) >>> tail $ 1..20 -- this line behaves the same