Skip to content

Instantly share code, notes, and snippets.

@dinoSpeech
Forked from dahlia/gist:3175089
Created July 25, 2012 11:06
Show Gist options
  • Save dinoSpeech/3175556 to your computer and use it in GitHub Desktop.
Save dinoSpeech/3175556 to your computer and use it in GitHub Desktop.
Higher-order function test suite
(define sum (make-aggregate + 0))
(sum (list 1 2 3)) ; 6
(define product (make-aggregate * 1))
(product (list 2 3 4)) ; 24
(define sum2 (make-aggregate (lambda (a b) (+ a b)) 0))
(sum2 (list 1 2 3)) ; 6
(define product2 (make-aggregate (lambda (a b) (* a b)) 1))
(product2 (list 2 3 4)) ; 24
(define (make-aggregate operate identity)
(define (aggregator lst)
(if (null? lst)
identity
(operate (car lst) (aggregator (cdr lst)))))
aggregator)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment