I am currently trying to create a function called reverse-with-count that returns a list of characters in reverse order, that are repeated a number of times as specified by the corresponding element of a second list of numbers.
For example:
(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)
I believe my else clause to be correct but I am getting errors in my code for the conditions I have set, where it is expecting real?
This is what I have done so far:
(define reverse-with-count
(lambda (ls1 ls2)
(cond
((null? ls2) ls1)
((positive? ls2) (error "Please enter a positive number!"))
((> ls1 ls2)(error "Please ensure the lists are the same size."))
((< ls1 ls2)(error "Please ensure the lists are the same size."))
(else
(cons (reverse (make-string (car ls2)(ls1))
(reverse-with-count (cdr ls2)(cdr ls1))))))))
How can I fix this issue?
make-string?>and<with lists. You want to compare the lengths of the lists, not the lists themselves.(not (= (length ls1) (length ls2)))(ls1)is wrong. That tries to callls1as a function, but it's a list.(make-string (car ls2) (car ls1)). Two problems: 1) the second argument tomake-stringmust be a character, not a symbol. 2) this will create a string like"ccc"not 3 separate list elements.