Note that the trivial solution
reverse a = foldr (\b c -> c ++ [b] ) [] a
is not very efficient, because of the quadratic growth in complexity. If have tried to use the usual foldl to foldr conversion (blindly), but my attempt
foldr (\b g x -> g ((\x old -> x:old) x b)) id list []
did not work as I expected.
Try this:
reverse bs = foldr (\b g x -> g (b : x)) id bs []
Though it's usually really better to write it using foldl':
reverse = foldl' (flip (:)) []
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With