Skip to content

Instantly share code, notes, and snippets.

@klandergren
Forked from gvinter/sicp2.17.scm
Created June 16, 2011 15:03
Show Gist options
  • Select an option

  • Save klandergren/1029430 to your computer and use it in GitHub Desktop.

Select an option

Save klandergren/1029430 to your computer and use it in GitHub Desktop.
SunnyCloud: 2.17 and 2.18
;; 2.17
;; Helpful link on equivalence
;; http://sicp.ai.mit.edu/Fall-2003/manuals/scheme-7.5.5/doc/scheme_4.html
(define (last-pair list)
(if (null? (cdr list))
(car list) ;; fixed paren error
(last-pair (cdr list))))
;; testing
(last-pair (list 1 2 3 4 5 6 4 3 23)) ;; => 23
;; 2.18 fails for me
(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