问题描述
我想编写一个程序来查找Scheme中二次方程的根.我使用LET进行某些绑定.
I want to write a program to find the roots of the quadratic equation in Scheme. I used LET for certain bindings.
(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))
,它也会给我这个错误:
I defined the discriminant with 4ac
since I did not want (* 4 a c)
. Even though I have defined (4ac (* 4 a c))
, it is giving me this error:
我的问题是如何评估(什么顺序)?如果我要在我的let
中使用4ac
,我应该再写一个内部let
吗?有更好的方法吗?
My question is how is let evaluated (what order)? And if i want 4ac
in my let
should i write another inner let
? Is there a better way to do this?
推荐答案
使用let*
代替let
.
let
和let*
之间的区别如下:
let*
从左到右绑定变量.可以将较早的绑定用于右侧(或向下)的新绑定中.
let*
binds variables from left to right. Earlier bindings can be used in new binding further to the right (or down).
let
可以被视为简单lambda抽象的语法糖(或宏):
let
on the other hand can be thought of as syntactic sugar (or macro) for simple lambda abstraction:
(let ((a exp1)
(b exp2))
exp)
等效于
((lambda (a b)
exp)
exp1 exp2)
这篇关于使用租用方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!