[back]

Fold Right 2

Implement the function

val fold_right2 : ('a -> 'b -> 'acc -> 'acc) -> 'a list -> 'b list -> 'acc -> 'acc

so that fold_right2 op l r base is the result of right-associatively folding op over the lists l and r simultaneously, e.g., fold_right2 op [a1;a2;a3;a4] [b1;b2;b3;b4] base is equivalent to op a1 b1 (op a2 b2 (op a3 b3 (op a4 b4 base))). The behavior of the function is undefined if l and r are not the same length.

Examples

let _ = assert (fold_right2 (fun x y acc -> x * y + acc) [1;2;3] [4;5;6] 0 = 32)
let _ = assert (fold_right2 (fun x y acc -> (x, y) :: acc) [1;2;3] ["a";"b";"c"] [] = [(1, "a"); (2, "b"); (3, "c")])
let _ = assert (fold_right2 (fun x y acc -> x ^ y ^ acc) ["a";"b"] ["c";"d"] "" = "acbd")