Skip to content

Instantly share code, notes, and snippets.

@yao2030
Last active December 16, 2015 02:09
Show Gist options
  • Save yao2030/5360463 to your computer and use it in GitHub Desktop.
Save yao2030/5360463 to your computer and use it in GitHub Desktop.
SICP Exercose 4.5, without peeking at others's code
(define (expand-clauses clauses)
(if (null? clauses)
'false ; no else clause
(let ((first (car clauses))
(rest (cdr clauses)))
(if (cond-else-clause? first)
(if (null? rest)
(sequence->exp (cond-actions first))
(error "ELSE clause isn't last -- COND->IF"
clauese))
(if (cond=>clause? first)
(let ((val (car first)))
(if val
(list (caddr first) val)
(make-if (cond-predicate first)
(sequence->exp (cond-actions first))
(expand-clauses rest)))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment