1
\$\begingroup\$

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))
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Sep 20, 2018 at 0:09
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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))))))
answered Sep 25, 2018 at 4:06
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.