[back]

Replicate

Implement the function

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

so that replicate l f n, given a list l, a function f and a positive integer n, returns the list where \(f^1, f^2, \dots, f^n\) are applied (in this order) to each element of l. For example, replicate [a;b] f 3 is

[f a; f (f a); f (f (f a)); f b; f (f b); f (f (f b))]

The behavior of the function is undefined if n is not positive.

Examples

let _ = assert (replicate [1;5;8;9] (fun x -> x + 1) 2 = [2;3;6;7;9;10;10;11])
let _ = assert (replicate [1;2;3] (fun x -> x * x) 1 = [1;4;9])