This code takes a list and a integer and duplicates every element in the list the specified number of times.
(define (super-duper source count)
(define (next-super source left)
(if (zero? left)
(super-duper (cdr source) count)
(cons (super-duper (car source) count) (next-super source (- left 1)))))
(if (pair? source)
(cons (super-duper (car source) count) (next-super source (- count 1)))
source))
(display(super-duper '((x y) t) 3))
1 Answer 1
I think you need to decide weather to repeat only on the first level of the list, or if you want to do a tree walk for lists inside lists. As is, this code produces a weird combination of both in an exponential explosion of the list, The following sample returns over 2 million leaf elements for an input of length 26 and a repeat of 10.
(leaf-count (super-duper '(a (b (c (d (e f g (h i) j k) l m n o) p q ) r (s t (u v) w ) x y) z) 10))
->> 2576420
(define (leaf-count Tree)
(cond ((null? Tree) 0)
((not (pair? Tree)) 1)
(else (+ (leaf-count (car Tree))
(leaf-count (cdr Tree))))))