Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Deep Reverse
; 2.27
(define (deep-reverse list)
(define (iter-list reversed-list unprocessed-remaining)
(if (null? unprocessed-remaining)
reversed-list
(iter-list (cons (if (list? (car unprocessed-remaining))
(deep-reverse (car unprocessed-remaining))
(car unprocessed-remaining))
reversed-list) (cdr unprocessed-remaining))
))
(iter-list null list)
)
(deep-reverse (list 3 4 (list 5 6))) ;--->((6 5) 4 3)
(deep-reverse (list 3 4 (list 5 6 (list 7 8 9)))) ;--->(((9 8 7) 6 5) 4 3)
(deep-reverse (list 3 4 (list 5 (list 6 (list 7))))) ;--->((((7) 6) 5) 4 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.