Skip to content

Instantly share code, notes, and snippets.

@ebresafegaga
Created December 6, 2020 15:38
Show Gist options
  • Save ebresafegaga/0ceddc94a5eade93e49414f58cb4a434 to your computer and use it in GitHub Desktop.
Save ebresafegaga/0ceddc94a5eade93e49414f58cb4a434 to your computer and use it in GitHub Desktop.
Possibly the world’s first functional pearl (by David Barron and Christopher Strachey) - Cartesian product
open ListLabels
let product xss =
let f xs yss =
let g x xs =
let h a b = (x :: a) :: b in
List.fold_right ~f:h ~init:xs yss
in
List.fold_right ~f:g ~init:[] xs
in
List.fold_right ~f:f ~init:[[]] xss
(* PS: it looks way nicer in Haskell because of where clauses *)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment