[Haskell-beginners] Repeat function application x times?

Ertugrul Soeylemez es at ertes.de
Sat Jul 23 08:26:33 CEST 2011


Christopher Howard <christopher.howard at frigidcode.com> wrote:

> Is there a convenient stock function in prelude/base that applies a
> function to a single value x number of times and returns the result?
> (I.e., does not return a list, but the final value.) Something like
>
> applyN :: Integral b => (a -> a) -> b -> a -> a
>
> Honestly I looked through the base hierarchy and at hoogle but I am
> missing it.

No, but it's easy to define:

    applyN :: Int -> (a -> a) -> a -> a
    applyN n = foldl (.) id . replicate n


Greets,
Ertugrul


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/





More information about the Beginners mailing list