Try the following:
(define (deep-reverse t) (let loop ((tt) (acc '())) (cond ((null? t) acc) ((not (pair? t)) t) (else (loop (cdr t) (cons (loop (car t) '()) acc))))))
Name it as follows:
(define stree (cons (list 1 2) (list 3 4))) (deep-reverse stree) > (4 3 (2 1))
To create an inverted list, one method is to accumulate the response in the parameter (usually I call it acc ). Since we are working on a list of lists, recursion must be called on both the car part and the cdr in the list. Finally, I use named let as a shorthand to avoid creating an additional function, but the same result can be obtained by defining a helper function with two parameters: a tree and a battery:
(define (deep-reverse t) (aux t '())) (define (aux t acc) (cond ((null? t) acc) ((not (pair? t)) t) (else (aux (cdr t) (cons (aux (car t) '()) acc)))))
source share