Values in PureScript

PureScript has various value types including strings, integers, floats, booleans, etc. Here are a few basic examples.

module Main where

import Prelude
import Effect (Effect)
import Effect.Console (log)

main :: Effect Unit
main = do
  -- Strings, which can be concatenated with <>
  log $ "pure" <> "script"

  -- Integers and floats
  log $ "1+1 = " <> show (1 + 1)
  log $ "7.0/3.0 = " <> show (7.0 / 3.0)

  -- Booleans, with boolean operators as you'd expect
  log $ show (true && false)
  log $ show (true || false)
  log $ show (not true)

To run this program, save it as Values.purs and use the PureScript compiler and runtime:

$ spago run
purescript
1+1 = 2
7.0/3.0 = 2.3333333333333335
false
true
false

In this PureScript example:

  1. We define a module named Main.
  2. We import necessary modules: Prelude for basic operations, Effect for handling side effects, and Effect.Console for logging to the console.
  3. The main function is defined as an Effect Unit, which is similar to IO () in Haskell.
  4. String concatenation is done using the <> operator instead of +.
  5. We use show to convert values to strings for logging.
  6. Boolean operations are similar to other languages.
  7. The log function is used instead of fmt.Println for console output.

PureScript, being a strongly-typed functional language, has some differences from imperative languages:

  • It uses <> for string concatenation instead of +.
  • Functions like log return an Effect, which represents side effects.
  • There’s no need for explicit type declarations in this example, as PureScript has strong type inference.

This example demonstrates basic value types and operations in PureScript, showing how it handles strings, numbers, and booleans in a functional programming context.