scheme - Maintaining list structure when duplicating -


i writing function duplicate items in list, list (a (b c)) becomes (a (b b c c)), function returns (a b b c c). how ensure retain internal list structure? here current code:

(define double   (lambda (l)     (cond ((null? l) '())           ((list? l) (append (double (car l)) (double (cdr l))))           (else (append (list l) (list l)) )     ) )) 

to preserve structure of list, have avoid using append. here implementation:

(define (double lst)   (cond     [(null? lst) empty]     [(list? (car lst))      (cons (double (car lst))            (double (cdr lst)))]     [else      (cons (car lst) (cons (car lst)            (double (cdr lst))))])) 

for example,

> (double '(a (b c) ((a b) (c d)))) '(a (b b c c) ((a b b) (c c d d))) 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -