2.2.7 deep-reverse
(define (next x) (cond ((not (pair? x)) x) ((pair? (car x)) (cons (deep-reverse (car x)) (next (cdr x)))) (else x)))(define (deep-reverse x) (reverse (next x)))
一开始我想采用一个函数直接搞定的方法
如,这两个都是有错误的,主要问题在于null返回也会被加到list中去,导致最终结果和预期的有偏差
(define (deep-reverse1 x) (cond ((pair? x) (cond ((null? (cdr x)) (deep-reverse1 (car x))) (else (list (deep-reverse1 (cdr x)) (deep-reverse1 (car x)))))) (else x)))(define (deep-reverse2 x) (cond ((pair? x) (cond ((null? (cdr x)) (deep-reverse2 (car x))) (else (cons (deep-reverse2 (cdr x)) (deep-reverse2 (car x)))))) (else x)))