I'm thinking of a function like this:
> let applyN (initial : 't) (n:int) (f : 't -> 't) = seq {1..n} |> Seq.fold (fun s _ -> f s) initial;;
val applyN : initial:'t -> n:int -> f:('t -> 't) -> 't
> applyN 0 10 (fun x -> x + 1);;
val it : int = 10
Note: The code is F# but I tagged the question with haskell, ocaml and ml tags because if the function doesn't exist in F# libraries but it exists in other languages I would like to use the same name
You would have gotten (very close to) an answer by using for example Hayoo (or Hoogle, but Hoogle's not as flexible — iterateN
was not found):
a search for Int -> (a -> a) -> a -> a
revealed several functions that do what you want but are not part of the stdlib.
a search for applyN
returned a function of exactly the same name with the type signature you are looking.
by laxing the return value by searching for Int -> (a -> a) -> a
instead (note the missing -> a
at the end), you get the iterateN :: Int -> (a -> a) -> a -> Seq a
function which erdeszt has already mentioned.
P.S. Hoogle seems to be more capable of flipping around argument order: (a -> a) -> Int -> a -> Seq a
successfully returns 'iterateN :: Int -> (a -> a) -> a -> Seq a`, which Hayoo does not.