Hello

I was surprised to be unable to find anything like this in the standard 
libraries:

times :: (a -> a) -> Int -> (a -> a)
times f 0 = id
times f n = f . (times f (n-1))

Am I missing something more general which would allow me to repeatedly apply a 
function to an input? Or is this not useful?

I thought this looked a bit like a fold, so I tried expressing it like this:

times f n x = foldl (flip ($)) x (replicate n f)

... which horrifies me, but I can't help feeling that there must be a way to 
get rid of the explicit recursion.

How would you implement times?

Ben


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to