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
Post a Comment