Skip to content

Instantly share code, notes, and snippets.

@aoeuidht
Created June 7, 2013 15:58
Show Gist options
  • Save aoeuidht/5730331 to your computer and use it in GitHub Desktop.
Save aoeuidht/5730331 to your computer and use it in GitHub Desktop.
exericse 1.32
(defun accumulate (combiner null-value term a next b)
(if (> a b)
null-value
(funcall combiner
(funcall term a)
(accumulate combiner null-value term (funcall next a) next b)))
)
(defun accumulate-iter (combiner null-value term a next b)
(defun iter (a result)
(if (> a b)
(print result)
(iter (funcall next a)
(funcall combiner
a
result))))
(iter a null-value))
(defun acc-sum (term a next b)
(accumulate #'+ 0 term a next b))
(defun acc-sum-iter (term a next b)
(accumulate-iter #'+ 0 term a next b))
(defun acc-product (term a next b)
(accumulate #'* 1 term a next b))
(defun acc-product-iter (term a next b)
(accumulate-iter #'* 1 term a next b))
(defun next (x) (+ x 1))
(acc-sum #'(lambda (x) x) 1 #'next 10)
(acc-sum-iter #'(lambda (x) x) 1 #'next 10)
(acc-product #'(lambda (x) x) 1 #'next 4)
(acc-product-iter #'(lambda (x) x) 1 #'next 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment