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.

Thanks much, that's exactly what I needed - that ++ concatentation operator. I was following examples too quickly and didn't realize that : is only for prepending.

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 →

So sexy!

Thanks much, that's exactly what I needed - that ++ concatentation operator. I was following examples too quickly and didn't realize that : is only for prepending.

using accumulator

`rev l = rev' l []`

rev'::[a]->[a]->[a]

rev' [] l = l

rev' (x:xs) l = rev' xs (x:l)

I m sorry, but what language is this?

Haskell

yo !

this is sorcery!

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... :-))Thanks! I forgot h was supposed to be a list.