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?

asked May 1, 2014 at 1:55
\$\endgroup\$

1 Answer 1

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.

answered May 1, 2014 at 10:51
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.