Skip to content

Instantly share code, notes, and snippets.

@gvinter
Forked from klandergren/sicp2.17.scm
Created June 16, 2011 14:52
Show Gist options
  • Save gvinter/1029390 to your computer and use it in GitHub Desktop.
Save gvinter/1029390 to your computer and use it in GitHub Desktop.
SunnyCloud: 2.17 and 2.18
;; 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