[back]

Cyclic Function Application

Implement the function

val apply_cycle : ('a -> 'a) list -> int -> 'a -> 'a

so that apply_cycle funcs n x is the result of applying n functions to x, where the functions applied are the functions in funcs in order from left to right, starting again from the beginning as necessary. For example, apply_cycle [f1;f2;f3] 5 x is equivalent to f2 (f1 (f3 (f2 (f1 x)))). Your implementation must be tail recursive. The behavior of the function is undefined if n is negative.

Examples

let f x = x + 1
let g x = x - 1
let h x = x * x
let k x = x / 2
let _ = assert (apply_cycle [f;g;g] 8 0 = -2)
let _ = assert (apply_cycle [g;f;f] 8 0 = 2)
let _ = assert (apply_cycle [f;g;g] 0 10 = 10)
let _ = assert (apply_cycle [f;h;k] 4 5 = 19)