Skip to content

Instantly share code, notes, and snippets.

@parallelepiped
Last active August 29, 2015 13:56
Show Gist options
  • Save parallelepiped/8951362 to your computer and use it in GitHub Desktop.
Save parallelepiped/8951362 to your computer and use it in GitHub Desktop.
SICP ex. 1.31
;helpers
(define (identity x) x)
(define (plus1 x) (+ 1 x))
; recursive
(define (product term a next b)
(if (> a b)
1
(* (term a)
(product term (next a) next b))))
(display (product identity 3 plus1 6)) ; 360
(newline)
; iterator
(define (product-i term a next b)
(define (prod-iter a current_prod)
(if (> a b)
current_prod
(prod-iter (next a) (* current_prod (term a)))))
(prod-iter a 1))
(display (product-i identity 3 plus1 6)) ; 360
(newline)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment