Last active
December 13, 2023 16:06
-
-
Save evancz/17cfb9ff531f839f82f050d05cd725b2 to your computer and use it in GitHub Desktop.
Syntax Hints (OCaml -> Elm)
This file contains 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
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