Skip to content

Instantly share code, notes, and snippets.

@thealmarty
Created January 1, 2019 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thealmarty/47b0bdf75262534b79fd503201596fe5 to your computer and use it in GitHub Desktop.
Save thealmarty/47b0bdf75262534b79fd503201596fe5 to your computer and use it in GitHub Desktop.
A zip function in OCaml that stops zipping when any of the input lists' first element is zero, or when any of the input list is empty.
(* Define the unfold function.*)
let rec unfold p g b1 b2 =
if p b1 b2 then [] else
(match g b1 b2 with (a, (b1prime, b2prime)) ->
a :: unfold p g b1prime b2prime)
;;
(* Define the zip_no_zero function.*)
let zip_no_zero = unfold
(* Define p.*)
(fun x y -> ((List.length x = 0) || (List.length y = 0) || (List.hd x = 0) ||
(List.hd y = 0) ))
(* Define g.*)
(fun x y -> ( (List.hd x, List.hd y), (List.tl x, List.tl y)))
;;
@thealmarty
Copy link
Author

See my blog post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment