Reputation: 31
I'm trying to review my final exam in R5RS, but having trouble with a simple problem. My professor isn't really helpful and I don't know anybody in my class. Can you help me?
The function ratio takes in two parameters f (function) and x (a number). I had to use a let statement. The scheme function is supposed to produced an outcome for:
f(x)+f(x+1)/f(x)
This is what I have so far:
(define (ratio f x)
(let ((f (+ x 1)))
(/ (+ x (+ f 1))
x)))
(ratio (lambda (x) (+ x 2)) 3)
I tried working with this for an hour, but still can't get the right answer.
Upvotes: 3
Views: 2365
Reputation: 31145
Math Scheme
f(x) (f x)
x+1 (+ x 1)
f(x+1) (f (+ x 1))
a/b (/ a b)
a/f(x) (/ a (f x))
f(x+1)/f(x) (/ (f (+ x 1)) (f x))
c + f(x+1)/f(x) ?
f(x) + f(x+1)/f(x) ?
Upvotes: 1
Reputation: 737
Hint: let a = f(x)
and let b = f(x + 1)
. What should the output be in terms of a
and b
?
In your solution, you bind f
to the value of x + 1
. So your solution is really calculating (x + (x + 2)) / x
. You need to apply f
to x
, ie (f x)
.
Here is a start:
(define (ratio f x)
(let ((a (f x)) (b (f (+ x 1))))
...))
Upvotes: 4