Hello fellow Haskellers.
Let's say I have an applicative functor (not an instance of monad) I want to apply multiple times to a pure initial value. For example,
λ> Just (1+) <*> (Just (1+) <*> pure 0)
Just 2
If I want to generalize this to any number of consecutive
applications, I can do it with a fold.
applyAppl :: Applicative f => f (a -> a) -> Int -> f a -> f a
applyAppl f n i = foldr (<*>) i $ replicate n f
After this definition,
λ> applyAppl (Just (1+)) 10 $ pure 0
Just 10
I have an awkward suspicion that the generalization could also be
done with one of the higher-order builtin applicative tools such as sequenceA or traverse. Can it?
(Edited to take into account the first two comments below.)
<*>to pull the function from applicative out to be a function in hask. - Jason Hu