Last active
October 29, 2019 22:44
-
-
Save chelseatroy/78247848353701e094a3903b6987b593 to your computer and use it in GitHub Desktop.
Deep Reverse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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