How do you solve this Haskell problem using a fold map and take?

Posted by Linda Cohen on Stack Overflow See other posts from Stack Overflow or by Linda Cohen
Published on 2010-05-06T01:47:23Z Indexed on 2010/05/06 1:58 UTC
Read the original article Hit count: 285

Define a function replicate which given a list of numbers returns a list with each number duplicated its value. Use a fold, map, and take

..> replicate [5,1,3,2,8,1,2]

output: [5,5,5,5,5,1,3,3,3,2,2,8,8,8,8,8,8,8,8,1,2,2]

I've figure this out using List comprehension and recursion:

replicate2 [] = []
replicate2 (n:nn) = take n(repeat n) ++ replicate2 nn

but how would you use fold and map to do this? so far I have: replicate n = map (foldl1 (take n(repeat n)) n) n which is obviously wrong, but I think I am close..

so any help would be nice, THANKS!

© Stack Overflow or respective owner

Related posts about homework

Related posts about haskell