Skip to content

Instantly share code, notes, and snippets.

@toctan
Last active December 31, 2015 19:39
Show Gist options
  • Select an option

  • Save toctan/8034749 to your computer and use it in GitHub Desktop.

Select an option

Save toctan/8034749 to your computer and use it in GitHub Desktop.
(define (sum-int a b)
(if (> a b)
0
(+ a
(sum-int (1+ a) b))))
(define (sum-sq a b)
(if (> a b)
0
(+ (square a)
(sum-sq (1+ a) b))))
(define (sum-pi a b)
(if (> a b)
0
(+ (/ 1 (* a (+ a 2)))
(sum-pi (+ a 4) b))))
;; Duplication!!
;; Abstract with procedure as first class citizen
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b))))
(define (sum-int a b)
(sum (lambda(i) i) a 1+ b))
(define (sum-sq a b)
(sum square a 1+ b))
(define (sum-pi a b)
(sum (lambda(i) (/ 1 (* i (+ i 4))))
a
(lambda(i) (+ i 4))
b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment