So I am going through some past papers for my Programming Languages module and I came across this question and I have no idea how to go about it.
Q: "Define a Scheme function reverse-with-count which takes two lists, the second of which is a list of non-negative integers the same length as the first list, and returns a list of elements from the first list, in reverse order, each repeated a number of times as specified by the corresponding element of the second list."
Examples:
(reverse-with-count '(a b c) '(1 2 3)) => (c c c b b a)
(reverse-with-count '(d c b a) '(3 0 0 1)) => (a d d d)
Thanks :)
Edit:
(define (repeat n s)
(if (= n 0)
'()
(append s
(repeat (- n 1) s))))
Using:
(repeat 10 '(test)) => '(test test test test test test test test test test)
consinstead ofappend, you can call the function with(repeat 10 'test). Also, be careful about possible negativenin inputs, you should probably use<=instead of=. But this is great. Now, What if you called(map repeat numbers symbols), wherenumbersandsymbolsare your lists of numbers and symbols? You would obtain a list of lists. Next, reverse that list, and concatenate all its elements with a(foldr append () ...). - coredump