首页 > 解决方案 > 方案:在这种情况下需要的变量

问题描述

我使用 mit-scheme 编译器来学习 Scheme。

我编写的程序必须通过 Vieta 定理计算方程根。

(define (roots p q x-begin x-end)
    (let ((x1 0.0) (x2 0.0))
        (set! (x1 x-begin)) ; Error here Variable required in this context: (x1 x-begin)
        (set! (x2 x-begin))

        ; ...  
    )   
)

我猜这个错误与 Scheme 中的静态范围有关。我做错了什么?

PS对不起我的英语。

标签: scheme

解决方案


我不确定您打算如何计算根,但我可以提供一些有关 Scheme 语法的建议,这是不正确的:

(set! (x1 x-begin))

它应该是:

(set! x1 x-begin)

通常,set!应尽可能避免使用:在 Scheme 中,我们非常努力地编写遵循函数式编程范式的程序,其中包括不重新分配变量。


推荐阅读