Skip to content

Instantly share code, notes, and snippets.

@kirang89
Last active August 29, 2015 14:12
Show Gist options
  • Save kirang89/39024dc3dc31d174abdd to your computer and use it in GitHub Desktop.
Save kirang89/39024dc3dc31d174abdd to your computer and use it in GitHub Desktop.
;
; Simple example to illustrate higher order procedures
;
(define (inc x) (+ x 1))
(define (cube x) (* x x x))
(define (even? x) (= (remainder x 2) 0))
(define (identity x) x)
; Recursive procedure performing a recursive process
(define (fn identity combiner term inc a b)
(if (> a b)
identity
(combiner (term a)
(fn identity combiner term inc (inc a) b))))
; Recursive procedure performing an iterative process
(define (fn1 identity combiner term inc a b)
(define (fn-iter combiner term inc a b result)
(if (> a b)
result
(fn-iter combiner term inc (inc a) b (combiner (term a)
result))))
(fn-iter combiner term inc a b identity))
(define (accumulate combiner null-value term inc a b)
(fn null-value combiner term inc a b))
; Testing
(accumulate + 0 cube inc 1 3)
(accumulate * 1 cube inc 1 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment