[Haskell-beginners] Does this function already exist in one of
the standard modules?
I. J. Kennedy
jack at realmode.com
Sat Aug 8 18:17:46 EDT 2009
I'm surprised there's no standard function for this.
Using iterate f x !! n is ok I suppose, but:
If I try to calculate the millionth fibonacci number like this
fibStep (a,b) = (b,a+b)
iterate fibStep (0,1) !! 1000000
I get a stack overflow, but if I use applyMany
applyMany 1000000 fibStep (0,1)
it works.
On Sat, Aug 8, 2009 at 3:38 PM, Felipe Lessa<felipe.lessa at gmail.com> wrote:
> On Sat, Aug 08, 2009 at 03:01:34PM -0500, I. J. Kennedy wrote:
>> Does this function already exist in one of the standard modules?
>> It computes f(f(f(...f(x))). I suspect it is a common function, but I
>> can't find it, even using Hoogle.
>>
>> applyMany :: Int -> (a -> a) -> a -> a
>> applyMany 0 f x = x
>> applyMany n f x = applyMany (n-1) f (f x)
>
> applyMany n f x = iterate f x !! n
>
> --
> Felipe.
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
More information about the Beginners
mailing list