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?
1 Answer 1
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.