Skip to content

Instantly share code, notes, and snippets.

@yao2030
Created December 18, 2012 08:25
Show Gist options
  • Save yao2030/4326107 to your computer and use it in GitHub Desktop.
Save yao2030/4326107 to your computer and use it in GitHub Desktop.
;;; SICP 2.58 part.b
;;;
;; sum
(define (make-sum a1 a2)
(cond ((and (number? a1) (number? a2)) (+ a1 a2))
((=number? a1 0) a2)
((=number? a2 0) a1)
(else (list a1 '+ a2))))
(define (sum? x)
(and (pair? x) (eq? (cadr x) '+)))
(define (addend s)
(car s))
(define (augend s)
(if (null? (cdddr s))
(caddr s)
(cddr s)))
;; product
(define (make-product m1 m2)
(cond
((or (=number? m1 0) (=number? m2 0)) 0)
((and (number? m1) (number? m2)) (* m1 m2))
((=number? m1 1) m2)
((=number? m2 1) m1)
(else (list m1 '* m2))))
(define (product? x)
(and (pair? x) (eq? (cadr x) '*)))
(define (multiplier e)
(car e))
(define (multiplicand p)
(if (null? (cdddr p))
(caddr p)
(cddr p)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment