gpt4 book ai didi

scheme - 在 Scheme 中使用 let

转载 作者:太空宇宙 更新时间:2023-11-03 18:33:56 26 4
gpt4 key购买 nike

我想在 Scheme 中编写一个程序来求二次方程的根。我将 LET 用于某些绑定(bind)。

(define roots-with-let
(λ (a b c)
(let ((4ac (* 4 a c))
(2a (* 2 a))
(discriminant (sqrt ( - (* b b) (4ac)))))
(cons ( / ( + (- b) discriminant) 2a)
( / ( - (- b) discriminant) 2a)))))

我用 4ac 定义了判别式,因为我不需要 (* 4 a c)。即使我已经定义了 (4ac (* 4 a c)),它还是给我这个错误:

expand: unbound identifier in module in: 4ac.

我的问题是 let 是如何求值的(什么顺序)?如果我想在我的 let 中使用 4ac,我应该编写另一个内部 let 吗?有更好的方法吗?

最佳答案

使用let*代替let

letlet* 的区别如下:

let* 从左到右绑定(bind)变量。较早的绑定(bind)可用于进一步向右(或向下)的新绑定(bind)。

另一方面,

let 可以被认为是简单 lambda 抽象的语法糖(或宏):

(let ((a exp1)
(b exp2))
exp)

相当于

((lambda (a b)
exp)
exp1 exp2)

关于scheme - 在 Scheme 中使用 let,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/946050/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com