Skip to content

Instantly share code, notes, and snippets.

@pdarragh
Created September 16, 2021 17:48
Show Gist options
  • Save pdarragh/131b7cf82101cb292a4e812cebdbae55 to your computer and use it in GitHub Desktop.
Save pdarragh/131b7cf82101cb292a4e812cebdbae55 to your computer and use it in GitHub Desktop.
OCaml Examples from Pierce's Office Hours (2021-09-16)
type color =
| Red
| Green
| Blue
| Black
type row = color list
type screen = row list
(* let rec count_red_row (cs : row) : int = *)
(* match cs with *)
(* | [] -> 0 *)
(* (\* option 1 *\) *)
(* | Red::cs' -> 1 + count_red_row cs' *)
(* | _::cs' -> count_red_row cs' *)
(* option 2 *)
(* | c::cs' -> *)
(* match c with *)
(* | Red -> 1 + count_red_row cs' *)
(* | _ -> count_red_row cs' *)
let rec count_red_screen (rs : screen) : int =
let rec count_red_row (cs : row) : int =
match cs with
| [] -> 0
(* option 1 *)
| Red::cs' -> 1 + count_red_row cs'
| _::cs' -> count_red_row cs'
in
match rs with
| [] -> 0
| r::rs' -> (count_red_row r) + (count_red_screen rs')
let foo x y z =
let sum_xy = x + y
in sum_xy + z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment