1
\$\begingroup\$

Here's how I implemented concat - flattens a list of lists into just a list of elements.

concat' :: [[a]] -> [a]
concat' ys = foldl (\acc x -> acc ++ x) [] ys

Is it a problem that I'm using the ++ function?

What's a better way to write this?

\$\endgroup\$
2
\$\begingroup\$

You could eta-contract the definition in two places: \acc x -> acc ++ x is the eta-expanded version of (++) and concat' ys = (...) ys is the eta-expanded version of concat' = (...). So that would take you to:

concat' :: [[a]] -> [a]
concat' = foldl (++) []

Now, in that case it turns out that it's better to use foldr rather than foldl and there is a good write-up about concat on the haskell wiki.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.