PureScript Cheat Sheet
Syntax, type classes, Effect/Aff for side effects, and Halogen basics for a strongly-typed functional language that compiles to JavaScript.
2 PagesAdvancedJan 16, 2026
Core Syntax
Bindings, type signatures, records, and ADTs.
haskell
module Main whereimport Prelude-- Type signature + bindinggreet :: String -> Stringgreet name = "Hello, " <> name-- Recordstype User = { name :: String, age :: Int }alice :: Useralice = { name: "Alice", age: 30 }older :: User -> Userolder u = u { age = u.age + 1 }-- Algebraic data typesdata Status = Loading | Success String | Failure Stringdescribe :: Status -> Stringdescribe = case _ of Loading -> "loading..." Success msg -> "ok: " <> msg Failure err -> "error: " <> err
Type Classes
PureScript's answer to ad-hoc polymorphism, with laws unlike TS interfaces.
haskell
class Describable a where describe :: a -> Stringinstance describableInt :: Describable Int where describe n = "Int: " <> show n-- Functor / Applicative / Monad for a custom typedata Box a = Box ainstance functorBox :: Functor Box where map f (Box a) = Box (f a)instance applyBox :: Apply Box where apply (Box f) (Box a) = Box (f a)instance applicativeBox :: Applicative Box where pure = Box
Effect & Aff
Effect models synchronous side effects; Aff models async, replacing callback hell.
haskell
import Effect (Effect)import Effect.Console (log)import Effect.Aff (Aff, launchAff_, delay)import Effect.Class (liftEffect)import Data.Time.Duration (Milliseconds(..))main :: Effect Unitmain = log "hello"asyncTask :: Aff UnitasyncTask = do liftEffect (log "starting") delay (Milliseconds 1000.0) liftEffect (log "done after 1s")runIt :: Effect UnitrunIt = launchAff_ asyncTask
Halogen Component (sketch)
A minimal Halogen component shape: State, Action, render, handleAction.
haskell
component :: forall q i o m. H.Component q i o mcomponent = H.mkComponent { initialState: \_ -> 0 , render , eval: H.mkEval H.defaultEval { handleAction = handleAction } } where render state = HH.div_ [ HH.button [ HE.onClick \_ -> Decrement ] [ HH.text "-" ] , HH.text (show state) , HH.button [ HE.onClick \_ -> Increment ] [ HH.text "+" ] ] handleAction = case _ of Increment -> H.modify_ (_ + 1) Decrement -> H.modify_ (_ - 1)
spago & PureScript Tooling
Common commands with spago, the standard build tool.
- spago init- scaffold a new project
- spago build- compile the project
- spago run- build and run the Main module
- spago test- run the test suite
- spago bundle-app- produce a single bundled JS file
- spago repl- interactive PSCi shell
- spago install <pkg>- add a dependency from the package set
- purs ide- IDE server used by editor plugins for type info
Pro Tip
Reach for newtype over type alias when you want compile-time distinction between semantically different values with the same runtime representation (e.g. UserId vs Int) — newtypes are erased at runtime so there's zero performance cost.
Was this cheat sheet helpful?
Explore Topics
#PureScript#PureScriptCheatSheet#Programming#Advanced#CoreSyntax#TypeClasses#EffectAff#HalogenComponentSketch#OOP#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance