Skip to content

Instantly share code, notes, and snippets.

@yao2030
Created April 11, 2013 03:00
Show Gist options
  • Save yao2030/5360311 to your computer and use it in GitHub Desktop.
Save yao2030/5360311 to your computer and use it in GitHub Desktop.
Derived expressions for and & or, SICP Ex4.4
(define (and->if exp)
(expand-and-clauses (and-clauses exp)))
(define (expand-and-clauses clauses)
(if (null? clauses)
true
(let ((first (car clauses))
(rest (cdr clauses)))
(if (false? first)
'f
(make-if first (expand-and-clauses rest) false)))))
(define (or->if exp)
(expand-if-clauses (or-clauses exp)))
(define (expand-or-clauses clauses)
(if (null? clauses)
false
(let ((first (car clauses))
(rest (cdr clauses)))
(if first
(make-if first true (expand-or-clauses rest))
true))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment