cd /news/developer-tools/syntax-hints-ocaml-elm · home topics developer-tools article
[ARTICLE · art-8544] src=gist.github.com ↗ pub= topic=developer-tools verified=true sentiment=· neutral

Syntax Hints (OCaml -> Elm)

Side-by-side comparison of syntax differences between OCaml and Elm, covering expressions, types, functions, type aliases, and algebraic data types (ADTs). It includes code examples showing how common OCaml constructs translate into Elm, such as string concatenation, list syntax, and type definitions. The summary is intended to help developers transitioning from OCaml to Elm understand key syntactic variations.

read2 min views23 publishedApr 19, 2022

ocaml.elm

  This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.

Learn more about bidirectional Unicode characters

Show hidden characters
module Hints exposing (..)

import List

import Html exposing (..)

import Html.Attributes as A

{-----------------------------------------------------------

      SYNTAX HINTS (OCaml -> Elm)

------------------------------------------------------------

EXPRESSIONS

| OCaml             | Elm                |
    |-------------------|--------------------|

    | "hello" ^ name    |  "hello" ++ name   |

    |  x + 1            |   x + 1            |

    |  x +. 1.0         |   x + 1            |

    |  ~-3              |   -3               |

    |  x <> 0           |   x /= 0           |

    |  [1;2;3]          |   [1,2,3]          |

TYPES

| OCaml             | Elm                |
    |-------------------|--------------------|

    | int               |  Int               |

    | string            |  String            |

    | 'a list           |  List a            |

    | 'a option         |  Maybe a           |

    | (int * int)       |  (Int, Int)        |

-----------------------------------------------------------}

{- FUNCTIONS -----------------------------------------------

let square (x : int) : int =

x * x

-----------------------------------------------------------}

square : Int -> Int

square x =

x * x

{- RECURSIVE FUNCTIONS -------------------------------------

let rec map (func : 'a -> 'b) (list : 'a list) : 'b list =

match list with

  | []    -> []

  | x::xs -> func x :: map func xs

-----------------------------------------------------------}

map : (a -> b) -> List a -> List b

map func list =

  case list of

    []    -> []

    x::xs -> func x :: map func xs

{- TYPE ALIASES --------------------------------------------

type person = { name : string; id : int }

-----------------------------------------------------------}

type alias Person = { name : String, id : Int }

{- ADTs ----------------------------------------------------

type color_label =

| Red

| Orange

| Yellow

| Green

| Blue

| Indigo

| Violet

type color = | Simple of color_label

| RGB of int * int * int

-----------------------------------------------------------}

type ColorLabel

= Red

| Orange

| Yellow

| Green

| Blue

| Indigo

| Violet

type Color = Simple ColorLabel

| RGB Int Int Int

{- More ADTs -----------------------------------------------

type 'a option =

| None

| Some of 'a

type 'a bintree = | Leaf

| Node of 'a * 'a bintree * 'a bintree

-----------------------------------------------------------}

type Maybe a

= Nothing

| Just a

type Tree a = Leaf

| Node a (Tree a) (Tree a)

── more in #developer-tools 4 stories · sorted by recency
── more on @ocaml 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/syntax-hints-ocaml-e…] indexed:0 read:2min 2022-04-19 ·