A reverse function with Monoid type class in haskell

207 views Asked by At

I try to write a list-corresponding reverse function under Monoid typeclass like this:

reverse :: [a] -> [a]
reverse ls = foldl ( \b a -> a:b ) [] ls

and my attempt is like this:

reverse :: (Foldable t, Applicative t, Monoid (t a)) => t a -> t a
reverse ls = foldl (\l r -> r `mappend` l ) mempty ls

But I received error message from console:

Occurs check: cannot construct the infinite type a ~ t a

I feel like I was missing something, any help will be appreciated.

0

There are 0 answers