As an introductory example, here is the usual "Hello World" written in PureScript:
module Main where
import Control.Monad.Eff.Console
main = log "Hello, World!"
The following code defines a Person
data type and a function to generate a string representation for a Person
:
data Person = Person { name :: String, age :: Int }
showPerson :: Person -> String
showPerson (Person o) = o.name <> ", aged " <> show o.age
examplePerson :: Person
examplePerson = Person { name: "Bonnie", age: 26 }
Line by line, this reads as follows:
Person
is a data type with one constructor, also calledPerson
- The
Person
constructor takes an object with two properties,name
which is aString
, andage
which is anInt
- The
showPerson
function takes aPerson
and returns aString
showPerson
works by case analysis on its argument, first matching the constructorPerson
and then using string concatenation and object accessors to return its result.examplePerson
is a Person object, made with thePerson
constructor and given the String "Bonnie" for the name value and the Int 26 for the age value.
The full language reference continues below: