We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

This is pretty inefficient haskell, as you have to traverse the entire list and make a copy of it each time you use the ++ operator. A more efficient solution is rev = foldl (\acc x -> x : acc) []. this way, you avoid the expense of using the ++ operator

Although, this is pretty advanced since we're making use of higher order functions already.
Deriving the 'inefficient' solution first, before learning why it's inefficient is the way to go.

yet I read on haskell.org/prelude, "Also note that if you want an efficient left-fold, you probably want to use foldl' instead of foldl." So maybe this isn't that efficient? Can you say a word about this? (I'm a complete newbie... :-))

## Reverse a List

You are viewing a single comment's thread. Return to all comments →

This is pretty inefficient haskell, as you have to traverse the entire list and make a copy of it each time you use the ++ operator. A more efficient solution is

`rev = foldl (\acc x -> x : acc) []`

. this way, you avoid the expense of using the ++ operatorOr, with

`flip`

:Although, this is pretty advanced since we're making use of higher order functions already. Deriving the 'inefficient' solution first, before learning why it's inefficient is the way to go.

yet I read on haskell.org/prelude, "Also note that if you want an efficient left-fold, you probably want to use

`foldl'`

instead of`foldl`

." So maybe this isn't that efficient? Can you say a word about this? (I'm a complete newbie... :-))