0

I am calculating a continuous fraction (The golden ratio for now) and this is my code:

(define cont-frac
  (lambda (n d k)
    (define res (+ (/ n d) n))
    (if (= k 0)
        res
        (cont-frac n res (- k 1)))))

And i call it like this:

(cont-frac 1.0 1.0 100)

which returns 1.618033988749895.

But my teacher wants me to call it like this:

(cont-frac (lambda (x) 1.0) 
           (lambda (x) 1.0) 
           100)

I am new to R5RS and stuff like this is not very clear to me yet. Why would I want to call it like this? How do i code my cont-frac function to allow this? Not sure if this info is helpful or not, but I am eventually going to want to use a function as one of the parameters.

Thanks in advance.

Sylwester
  • 47,942
  • 4
  • 47
  • 79
pootyy
  • 55
  • 6
  • I ant see why they'd want you do this, given the lambdas are just evaluating to constants. They may be *trying* to teach you higher-order functions. Just call `n` and `d` inside the function by wrapping parentheses around each before trying to use them, as you would any function. – Carcigenicate Jan 21 '17 at 19:57
  • What is the one argument suppose to be? `k`? the previous value of `d`? – Sylwester Jan 21 '17 at 23:05

1 Answers1

0

your teacher probably wants you to be able to evaluate d and k based on n.
this kind of usage lets you choose the way you evaluate the step's error every time you call cont-frac.

that's the way it should probably look:

(define cont-frac
  (lambda (n d k)
    (define x n) ;; assuming you want to evaluate d and k based on n
    (define res (+ (/ n (d x)) n))
    (if (= (k x) 0)
        res
        (cont-frac n res (lambda (x) (- (k x) 1))))))