Created
January 1, 2019 23:14
-
-
Save thealmarty/5fbe80c8b3a4cab45d2ef913d5574a84 to your computer and use it in GitHub Desktop.
A modified zip function in OCaml that sums up the two elements of the input lists in the second item of the output tuple.
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
(* 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_sum function.*) | |
let zip_sum = unfold | |
(* Define p.*) | |
(fun x y -> ((List.length x = 0) || (List.length y = 0))) | |
(* Define g.*) | |
(fun x y -> ( (List.hd x, (List.hd x + List.hd y)), (List.tl x, List.tl y))) | |
;; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See my blog post.