Created
October 29, 2019 14:58
-
-
Save chelseatroy/5181cb4348da7061ac2834c7ae9c671c to your computer and use it in GitHub Desktop.
Higher Order Procedures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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