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 ++ operator
rev = foldl (\acc x -> x : acc) 
Or, with flip:
rev = foldl (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... :-))