尝试在自己中调用函数时,我收到一个未定义的函数错误。
我已经测试了两个猜测好吗?并且更好地猜测seperatley并且它们完美地工作。然后我调用(square_root 1 2)来尝试找到2的平方根。
;;;;program to find square root
(defun sqaure_root (guess x)
(if (guess-okay? guess x)
guess
(square_root (better-guess guess x) x)))
(defun guess-okay? (guess x)
"returns true if guess is within one 0.001 of value of square root"
(> 0.001 (abs-x (- (square-x guess) x))))
(defun better-guess (guess x)
"takes a guess and makes it a more accurate guess using newtons method of succsesive apporximations"
(/ (+ guess (/ x guess)) 2))
错误是'undefined function square_root',我不明白因为我已经定义了它。我认为这个问题与调用函数中的函数有关,但我不确定。
发布于 2019-07-17 16:51:58
正如其他人所提到的,您的函数定义不正确。您的代码应如下所示:
;;;;program to find square root
(defun square_root (guess x)
(if (guess-okay? guess x)
guess
(square_root (better-guess guess x) x)))
(defun guess-okay? (guess x)
"returns true if guess is within one 0.001 of value of square root"
(> 0.001 (abs-x (- (square-x guess) x))))
(defun better-guess (guess x)
"takes a guess and makes it a more accurate guess using newtons method of succsesive apporximations"
(/ (+ guess (/ x guess)) 2))
你在函数定义中拼错了“square”,这就是为什么“square_root”被认为是未定义的。
https://stackoverflow.com/questions/-100007076
复制相似问题