{"slug": "syntax-hints-ocaml-elm", "title": "Syntax Hints (OCaml -> Elm)", "summary": "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.", "body_md": "ocaml.elm\n\n      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.\n      \nLearn more about bidirectional Unicode characters\n\n \n    Show hidden characters\n\nmodule Hints exposing (..)\n\nimport List\n\nimport Html exposing (..)\n\nimport Html.Attributes as A\n\n{-----------------------------------------------------------\n\n      SYNTAX HINTS (OCaml -> Elm)\n\n------------------------------------------------------------\n\n  EXPRESSIONS\n\n    | OCaml             | Elm                |\n\n    |-------------------|--------------------|\n\n    | \"hello\" ^ name    |  \"hello\" ++ name   |\n\n    |  x + 1            |   x + 1            |\n\n    |  x +. 1.0         |   x + 1            |\n\n    |  ~-3              |   -3               |\n\n    |  x <> 0           |   x /= 0           |\n\n    |  [1;2;3]          |   [1,2,3]          |\n\n  TYPES\n\n    | OCaml             | Elm                |\n\n    |-------------------|--------------------|\n\n    | int               |  Int               |\n\n    | string            |  String            |\n\n    | 'a list           |  List a            |\n\n    | 'a option         |  Maybe a           |\n\n    | (int * int)       |  (Int, Int)        |\n\n-----------------------------------------------------------}\n\n{- FUNCTIONS -----------------------------------------------\n\nlet square (x : int) : int =\n\n  x * x\n\n-----------------------------------------------------------}\n\nsquare : Int -> Int\n\nsquare x =\n\n  x * x\n\n{- RECURSIVE FUNCTIONS -------------------------------------\n\nlet rec map (func : 'a -> 'b) (list : 'a list) : 'b list =\n\n  match list with\n\n  | []    -> []\n\n  | x::xs -> func x :: map func xs\n\n-----------------------------------------------------------}\n\nmap : (a -> b) -> List a -> List b\n\nmap func list =\n\n  case list of\n\n    []    -> []\n\n    x::xs -> func x :: map func xs\n\n{- TYPE ALIASES --------------------------------------------\n\ntype person = { name : string; id : int }\n\n-----------------------------------------------------------}\n\ntype alias Person = { name : String, id : Int }\n\n{- ADTs ----------------------------------------------------\n\ntype color_label =\n\n  | Red\n\n  | Orange\n\n  | Yellow\n\n  | Green\n\n  | Blue\n\n  | Indigo\n\n  | Violet\n\ntype color =\n\n  | Simple of color_label\n\n  | RGB of int * int * int\n\n-----------------------------------------------------------}\n\ntype ColorLabel\n\n  = Red\n\n  | Orange\n\n  | Yellow\n\n  | Green\n\n  | Blue\n\n  | Indigo\n\n  | Violet\n\ntype Color\n\n  = Simple ColorLabel\n\n  | RGB Int Int Int\n\n{- More ADTs -----------------------------------------------\n\ntype 'a option =\n\n  | None\n\n  | Some of 'a\n\ntype 'a bintree =\n\n  | Leaf\n\n  | Node of 'a * 'a bintree * 'a bintree\n\n-----------------------------------------------------------}\n\ntype Maybe a\n\n  = Nothing\n\n  | Just a\n\ntype Tree a\n\n  = Leaf\n\n  | Node a (Tree a) (Tree a)", "url": "https://wpnews.pro/news/syntax-hints-ocaml-elm", "canonical_source": "https://gist.github.com/evancz/17cfb9ff531f839f82f050d05cd725b2", "published_at": "2022-04-19 21:21:42+00:00", "updated_at": "2026-05-22 11:39:18.919522+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["OCaml", "Elm"], "alternates": {"html": "https://wpnews.pro/news/syntax-hints-ocaml-elm", "markdown": "https://wpnews.pro/news/syntax-hints-ocaml-elm.md", "text": "https://wpnews.pro/news/syntax-hints-ocaml-elm.txt", "jsonld": "https://wpnews.pro/news/syntax-hints-ocaml-elm.jsonld"}}