-
-
Save gvinter/1029390 to your computer and use it in GitHub Desktop.
SunnyCloud: 2.17 and 2.18
This file contains 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.17 | |
(define (last-pair list) | |
(if (= (cdr list) () | |
(car list) ;; if statement parens fixed | |
(last-pair (cdr list)) ;; now it's called last-pair recursively | |
;; 2.18 | |
(define (reverse list) | |
(if (= (cdr list) ()) ;; if statement inserted | |
(= new-list (cons (car list) ()))) ;; extra paren added for if statement | |
(cons (car list) (car new-list))) | |
;; Think of 2.18 in terms of 2.17: if you can find the last pair of a list | |
;; can't you call that recursively to build a new list last-element to first? | |
;; Testing | |
(reverse (list 1 4 9 16 25)) ;; => (list 25 26 9 4 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment