-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_1_30.clj
31 lines (27 loc) · 877 Bytes
/
ex_1_30.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
(ns sicp.chapter-1.part-3.ex-1-30)
; Exercise 1.30
; The sum procedure above generates a linear recursion.
; The procedure can be rewritten so that the sum is performed iteratively.
; Show how to do this by filling in the missing expressions in the following definition:
; Racket
; (define (sum term a next b)
; (define (iter a result)
; (if ⟨??⟩
; ⟨??⟩
; (iter ⟨??⟩ ⟨??⟩)))
; (iter ⟨??⟩ ⟨??⟩))
; Clojure
; (defn sum [term a next b]
; (letfn [(iter [a result]
; (if ⟨??⟩
; ⟨??⟩
; (iter ⟨??⟩ ⟨??⟩)))]
; (iter ⟨??⟩ ⟨??⟩)))
(defn sum-2
[term a next-fn b]
(letfn [(iter
[a result]
(if (> a b)
result
(iter (next-fn a) (+ (term a) result))))]
(iter a 0)))