尝试在itelf中调用函数时出现未定义的函数错误。
我都测试了两个猜测,好吗?和猜测更好的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))
错误是“未定义的函数square_root”,我不明白,因为我已经定义了它。我相信问题与在函数中调用函数有关,但我不确定。
答案 0 :(得分:0)
正如其他人提到的,您有错误的函数定义。您的代码应如下所示:
;;;;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”被视为未定义的原因。