0

Once using direct style, once using foldr and once using list comprehensions to write the function of concatMap.

Following function was i wrote by foldr but it has some problems.**

  concatMap' :: (a -> [b]) -> [a] ->[b] 
  concatMap' f []=[] 
  concatMap' f (x:xs)==foldr (\x acc->acc : f x) [] xs

2 Answers 2

2

Since you are supposed to be using foldr, you will not be writing an explicit recursive definition of concatMap'. Thus, you only need one definition. To use foldr, think about what the accumulator will contain. What kind of state do you need to keep between iterations? How is that state updated for each list element? What should the final value returned be? You might want to write the code in direct style (which you mentioned was also part of your assignment) first so that you understand the recursion in detail, then try to match that to the definition of foldr (its source code is in the Haskell 98 specification or you can search for it).

Sign up to request clarification or add additional context in comments.

Comments

0
   foldr op z [ a', b', c', d', .. ]
== a' `op` b' `op` c' `op` d' `op` .. `op` z

What values of op, z, and [ a', b', c', d', .. ] would make the result look like the expected outcome of concatMap f [a, b, c, d, ..]?

Comments

Your Answer

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.