I am new to Haskell.
I am trying to write a program which given a list as an input replicates each element of list k times, where k = position of element in list.
e.g. replic[5,6,7] gives [[5],[6,6],[7,7,7]].
Another condition is solution has to use map function.
Till now code I have written is :
replic [] = []
replic (x:xs) = map (replicate 2 ) [x] ++ replic xs
This replicates every element twice as replicate has input parameter 2.
What I need is replicate function should be given input as 1 ,2 ,3 in consecutive calls. So I need a counter. How can I use the counter there or do anything else that will give me position of element?
Expanding on Satvik, the notation
gives you an infinite list of numbers counting up.
The function
zipassociates allows you to merge two lists into a list of tuplesfor example
this code associates each value in the list with its position in the list
now
repeats a value an arbitrary number of times. Given these two components, we can design a simple function
which I would write pointfree as
this does exactly what you want