2.27

练习 2.27 修改练习2.18中所做的reverse过程,得到一个deep-reverse过程。它以一个表为参数,返回另一个表作为值,结果表中的元素反过来,其中的子树也反转。例如:

(define x (list (list 1 2) (list 3 4)))
x
((1 2) (3 4))

(reverse x)
((3 4) (1 2))

(deep-reverse x)
((4 3) (2 1))

(define (append list1 list2)
  (if (null? list1)
    list2
    (cons (car list1) (append (cdr list1) list2)))
)

(define (reverse lst) 
  (if (null? (cdr lst))
      lst
      (append (reverse (cdr lst)) (list (car lst)))))

(define x (list (list 1 2) (list 3 4)))
x
(reverse x)
(define (deep-reverse lst) (reverse (map reverse lst)))

(deep-reverse x)

results matching ""

    No results matching ""