Skip to content

Instantly share code, notes, and snippets.

@parallelepiped
Created March 17, 2014 17:08
Show Gist options
  • Save parallelepiped/9603693 to your computer and use it in GitHub Desktop.
Save parallelepiped/9603693 to your computer and use it in GitHub Desktop.
SICP questions 2.17 and 2.18
;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