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

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -