list - Scheme: advise on implementation of flatten -
my implementation of flatten
looks this:
(define flatten (lambda (lst) (if (null? lst) lst (append (rtn-lst (car lst)) (flatten (cdr lst)))))) (define rtn-lst (lambda (lst) (cond ((null? lst) empty) ((atom? lst) (list lst)) (else (flatten lst)))))
while standard implementation is:
(define (flatten lst) (cond ((null? list) empty) ((list? (car lst)) (append (flatten (car lst)) (flatten (cdr lst)))) (else (cons (car lst) (flatten (cdr lst))))))
apart obvious verboseness, else wrong code?
i'd try this:
(define rtn-lst (lambda (lst) (cond ((list? lst) (if (null? lst) empty (flatten-list lst))) ((atom? lst) (list lst)) (else (flatten-list lst)))))
probably have different implementations of scheme.
edit:
with modified else
branch:
(define rtn-lst (lambda (lst) (cond ((list? lst) (if (null? lst) empty (flatten-list lst))) (else (list lst)))))
Comments
Post a Comment