1

Typeclassopedia (Haskell Wiki) has this exercise in Applicative's section:

Implement a function
sequenceAL :: Applicative f => [f a] -> f [a]

I could only do it, if used the do notation (requiring Monad f to be added to the context:

sequenceAL :: (Monad f, Applicative f) => [f a] -> f [a]
sequenceAL [] = pure []
sequenceAL (x:xs) = do
                     y <- x
                     ys <- sequenceAL xs
                     pure ([y] ++ ys)

How do I achieve this without using the do-notation and monads.

coder_bro
  • 10,503
  • 13
  • 56
  • 88

1 Answers1

4

It can use <$> and <*> operator in Control.Applicative to do it:

sequenceAL :: Applicative f => [f a] -> f [a]
sequenceAL [] = pure []
sequenceAL (x:xs) = (:) <$> x <*> sequenceAL xs 
assembly.jc
  • 2,056
  • 1
  • 7
  • 16