Functions in PureScript

Functions are central in PureScript. We’ll learn about functions with a few different examples.

module Main where

import Effect.Console (log)

Here’s a function that takes two Ints and returns their sum as an Int.

plus :: Int -> Int -> Int
plus a b = a + b

PureScript uses implicit returns, so the last expression in a function is automatically returned.

When you have multiple parameters, you can define them separately or use currying, which is more idiomatic in PureScript.

plusPlus :: Int -> Int -> Int -> Int
plusPlus a b c = a + b + c
main :: Effect Unit
main = do

Call a function just as you’d expect, with name args.

  let res = plus 1 2
  log $ "1+2 = " <> show res

  let res' = plusPlus 1 2 3
  log $ "1+2+3 = " <> show res'

To run the program, you would typically use the PureScript compiler (purs) and Node.js:

$ spago run
1+2 = 3
1+2+3 = 6

There are several other features to PureScript functions. One is currying, which we’ve implicitly used in our examples above.