Writing a function that finds the smallest k such that the difference between x and a function

感情迁移 提交于 2020-02-06 16:56:15

问题


I am having trouble where my code feels incomplete and plain wrong. For my function (terms-needed x tol) I am supposed to find the smallest k such that the difference between x and (square (babylonian x k)) is less than tol (tolerance). In other words we are supposed to measure how large k needs to be in the function (babylonian x k) to provide a good approximation of the square root.

As of right now I am getting an error of "application: not a procedure;" with my code

(define (square x)
  (* x x))

(define (first-value-k-or-higher x tol k)
  (if (<= (x)
         (square (babylonian x k)) tol)
      k)
      (first-value-k-or-higher x tol (+ k 1))
  )


(define (terms-needed x tol)
  (first-value-k-or-higher x tol 1))

We are supposed to use a helper-function (first-value-k-or-higher x tol k) that evaluates to k if (square (bablyonian x k)) is within tol of the argument x, otherwise calls itself recursively with larger k.

This is the function that is needed to make (terms-needed x tol) work:

  (define (babylonian x k)
    (if (>= x 1)
      (if (= k 0)
          (/ x 2)
          (* (/ 1 2) (+ (expt x (/ 1 2)) (/ x (expt x (/ 1 2))))))
      1)
  )

Here is the full text, providing the full context on what the problem is supposed to be.

We will now measure how large k needs to be in the above function to provide a good approximation of the square root. You will write a SCHEME function (terms-needed x tol) that will evaluate to the number of terms in the infinite sum needed to be within tol, that is, the smallest k such that the difference between x and (square (babylonian x k)) is less than tol. Remark 2. At first glance, the problem of defining (terms-needed x tol) appears a little challenging, because it’s not at all obvious how to express it in terms of a smaller problem. But you might consider writing a helper function (first-value-k-or-higher x tol k) that evaluates to k if (square (bablyonian x k)) is within tol of the argument x, otherwise calls itself recursively with larger k.


回答1:


You have several problems.

First, you have parentheses around x in

 (if (<= (x)

That's causing the error you're seeing, because it's trying to call the function named x, but x names a number, not a function.

Second, you're not calculating the difference between x and (square (babylonian x k)). Instead, you gave 3 arguments to <=.

Third, you're not making the recursive call when the comparison fails. It's outside the if, so it's being done all the time (if you make use of an editor's automatic indentation feature, you might have noticed this problem yourself).

Fourth, you need to get the absolute value of the difference, not just the difference itself. Otherwise, if the difference is a large negative number, you'll consider it within the tolerance, which it shouldn't be.

(define (first-value-k-or-higher x tol k)
  (if (<= (abs (- x (square (babylonian x k)))) 
          tol)
      k
      (first-value-k-or-higher x tol (+ k 1))))


来源:https://stackoverflow.com/questions/52340562/writing-a-function-that-finds-the-smallest-k-such-that-the-difference-between-x

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!