what are elm and purescript?

Software Development

Elm and PureScript are both strongly typed, purely functional programming languages that compile to JavaScript. While they share the same foundational goal of ensuring robust and error-free web applications by preventing runtime exceptions, they differ significantly in their design philosophies, learning curves, and feature sets.

Elm is an opinionated, highly beginner-friendly language designed specifically for building web user interfaces. It enforces a strict architectural pattern, known as The Elm Architecture (TEA), which unifies the way state, updates, and HTML are handled. Elm is famous for having a gentle learning curve, clean syntax, and exceptionally helpful compiler error messages that guide developers on exactly how to fix their code. Elm’s type system guarantees that runtime exceptions are practically non-existent. It is exclusively for front-end web development. You cannot use Elm to build backends or command-line tools. The language is intentionally designed to lack certain advanced functional abstractions (like typeclasses) to keep the codebase simple and uniform.

PureScript is an advanced, general-purpose functional language that strongly resembles Haskell. It is highly expressive and allows developers to write extremely complex and reusable abstractions. PureScript is an advanced, general-purpose functional language that strongly resembles Haskell. It is highly expressive and allows developers to write extremely complex and reusable abstractions. If you are already familiar with Haskell, PureScript will feel very natural, as it includes powerful features like typeclasses and higher-kinded types. Unlike Elm, PureScript can be used on both the front-end (for web UIs using libraries like Halogen) and the back-end (e.g., in Node.js). It features a robust Foreign Function Interface (FFI) that makes communicating with existing JavaScript libraries and codebases much easier. Because of its advanced type system and high flexibility, PureScript can be more difficult to learn and manage than Elm.

Example Code

Here is an example of a simple counter in both Elm and PureScript:

Elm

module Main exposing (main)

import Browser
import Html exposing (Html, button, div, text)
import Html.Events exposing (onClick)

-- MODEL
type alias Model = Int

init : Model
init = 0

-- UPDATE
type Msg = Increment | Decrement

update : Msg -> Model -> Model
update msg model =
    case msg of
        Increment -> model + 1
        Decrement -> model - 1

-- VIEW
view : Model -> Html Msg
view model =
    div []
        [ button [ onClick Decrement ] [ text "-" ]
       , div [] [ text (String.fromInt model) ]
       , button [ onClick Increment ] [ text "+" ]
        ]

main =
    Browser.sandbox { init = init, update = update, view = view }

PureScript

module Main where

import Prelude
import Effect (Effect)
import Halogen as H
import Halogen.Aff as HA
import Halogen.HTML as HH
import Halogen.HTML.Events as HE
import Halogen.VDom.Driver (runUI)

type State = Int

data Action = Increment | Decrement

main :: Effect Unit
main = HA.runHalogenAff do
  body <- HA.awaitBody
  runUI component unit body

component :: forall query input output m. H.Component query input output m
component =
  H.mkComponent
    { init: \_ -> 0
   , eval: H.mkEval $ H.defaultEval { handleAction = handleAction }
   , render
    }

render :: forall slots m. State -> H.ComponentHTML Action slots m
render state =
  HH.div_
    [ HH.button [ HE.onClick \_ -> Decrement ] [ HH.text "-" ]
   , HH.div_ [ HH.text (show state) ]
   , HH.button [ HE.onClick \_ -> Increment ] [ HH.text "+" ]
    ]

handleAction :: forall output m. Action -> H.HalogenM State Action slots output m Unit
handleAction = case _ of
  Increment -> H.modify_ \state -> state + 1
  Decrement -> H.modify_ \state -> state - 1
Back to all articles