Reputation: 599
In SICP, (1.3.2. page: 62), there is a solution for finding pi-sum
using lambda. The solution is:
(define (pi-sum a b)
(sum (lambda (x) (/ 1.0 (* x (+ x 2))))
a
(lambda (x) (+ x 4))
b))
However, I feel there should be a bracket enclosing ((lambda (x) (+ x 4) b)
. The program as such produces an error saying sum
is expecting a number
but getting a procedure
.
Modifying the above code is giving no error.
(define (pi-sum a b)
(sum ((lambda (x) (/ 1.0 (* x (+ x 2))))
a)
((lambda (x) (+ x 4))
b)))
Please correct me if my understanding is wrong. I assume the book cannot be wrong.
Upvotes: 1
Views: 101
Reputation: 21317
The pi-sum
procedure in the book is making use of the higher-order procedure sum
, defined earlier in 1.3.1. The sum
procedure takes a
and b
as arguments which describe the bounds of the summation, and it takes term
and next
as arguments which describe how to create a term from a
, and how to get the next a
from the current a
. Both term
and next
need to be procedures. Here is the definition of sum
from the book:
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b))))
If you add parentheses in the definition of pi-sum
, you should get an exception because sum
requires four arguments, but now only two are passed. I am not sure why you are getting an error like "sum
is expecting a number
but getting a procedure
", but I suspect that you have a different definition for sum
than the one intended by the book.
Upvotes: 4