Skip to content

Instantly share code, notes, and snippets.

@evancz
Last active December 13, 2023 16:06
Show Gist options
  • Save evancz/17cfb9ff531f839f82f050d05cd725b2 to your computer and use it in GitHub Desktop.
Save evancz/17cfb9ff531f839f82f050d05cd725b2 to your computer and use it in GitHub Desktop.
Syntax Hints (OCaml -> Elm)
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment