Skip to content

Instantly share code, notes, and snippets.

@chelseatroy
Created October 29, 2019 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chelseatroy/5181cb4348da7061ac2834c7ae9c671c to your computer and use it in GitHub Desktop.
Save chelseatroy/5181cb4348da7061ac2834c7ae9c671c to your computer and use it in GitHub Desktop.
Higher Order Procedures
(define (sum term a next b)
(accumulate + 0 term a next b))
(define (product term a next b)
(accumulate * 1 term a next b))
(define (factorial n)
(product identity 1 inc n))
(define (accumulate combiner base-case term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (combiner (term a) result))))
(iter a base-case))
(define (identity x) x)
(define (inc x) (+ 1 x))
(accumulate * 1 identity 1 inc 5) ; 120
(factorial 5) ; 120
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment