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. 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