Variables in PureScript

Here’s the translated code from Go to PureScript, along with explanations in Markdown format suitable for Hugo:

In PureScript, variables are explicitly declared and used by the compiler to check type-correctness of function calls.

module Main where

import Effect.Console (log)

main :: Effect Unit
main = do
  -- `let` declares a variable within a do block
  let a = "initial"
  log a

  -- You can declare multiple variables at once
  let b = 1
      c = 2
  log $ show b <> " " <> show c

  -- PureScript infers the type of initialized variables
  let d = true
  log $ show d

  -- Variables declared without initialization are not allowed in PureScript
  -- Instead, we can use Maybe type to represent potential absence of value
  let e :: Maybe Int
      e = Nothing
  log $ show e

  -- In PureScript, all bindings are immutable by default
  let f = "apple"
  log f

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

$ pulp run
initial
1 2
true
Nothing
apple

In PureScript:

  1. Variables are declared using let within a do block or at the top level of a module.
  2. Multiple variables can be declared in a single let block.
  3. Type inference is used, but you can also explicitly specify types.
  4. There’s no concept of “zero values” like in some other languages. Instead, you might use the Maybe type to represent potentially absent values.
  5. All bindings in PureScript are immutable by default, so there’s no equivalent to the := syntax for reassignment.
  6. Functions like print or println are replaced with log from the Effect.Console module.

PureScript is a strongly-typed, purely functional language, so it handles variables and state differently from imperative languages. The focus is on immutability and explicit handling of side effects.