Created
March 17, 2014 17:08
-
-
Save parallelepiped/9603693 to your computer and use it in GitHub Desktop.
SICP questions 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 | |
;(last-pair (list 23 72 89 124 34)) ; should be (34) | |
(define (last-pair x) | |
(if (atom? (cdr x)) | |
x | |
(last-pair (cdr x)))) | |
(display (last-pair (list 23 72 89 124 34))) | |
(newline) | |
; map-reduciness? Seems more like functional while loop | |
; 2.18 | |
(define (reverse-iter newl oldl) | |
(if (atom? oldl) | |
newl | |
(reverse-iter (cons (car oldl) newl) (cdr oldl)))) | |
; (reverse (list 1 2 3 4 5)); should be (5 4 3 2 1) | |
(define (reverse x) | |
(reverse-iter `() x)) | |
(display (reverse (list 1 2 3 4 5))) | |
(newline) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment