Multiple Return Values in PureScript

PureScript has built-in support for multiple return values through tuples. This feature is often used in idiomatic PureScript, for example to return both result and error values from a function.

module Main where

import Effect.Console (log)

-- The Tuple Int Int in this function signature shows that
-- the function returns a tuple containing 2 Ints.
vals :: Tuple Int Int
vals = Tuple 3 7

main :: Effect Unit
main = do
  -- Here we use pattern matching to extract the two different 
  -- return values from the tuple.
  let (Tuple a b) = vals
  log $ show a
  log $ show b

  -- If you only want a subset of the returned values,
  -- you can use wildcards in pattern matching.
  let (Tuple _ c) = vals
  log $ show c

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

$ pulp run
3
7
7

PureScript’s type system and pattern matching provide a powerful way to work with multiple return values. The next concept we might explore is how PureScript handles functions with variable numbers of arguments.