[Haskell-cafe] Repeated function application

jerzy.karczmarczuk at info.unicaen.fr jerzy.karczmarczuk at info.unicaen.fr
Thu Feb 21 11:06:58 EST 2008


Ben Butler-Cole writes: 

> times :: (a -> a) -> Int -> (a -> a)
> times f 0 = id
> times f n = f . (times f (n-1)) 
> 
> Am I missing something more general
> ...I can't help feeling that there must be a way to get rid of the 
>  explicit recursion. 
> 
> How would you implement times?

Anything against (apart an auxiliary list, and "x" not curried away) 

times n f x = (iterate f x)!!n 

Jerzy Karczmarczuk 




More information about the Haskell-Cafe mailing list