使用“和”在方案中
嘿,我正在尝试在 cond
语句中使用 and
。基本上,我需要在运行某些代码之前检查
是否为 true,而不是简单地检查
AND
是真的。我知道 (and #t #f)
的计算结果为 #f
并且 (and (= 10 (* 2 5)) #t)
计算结果为#t
。不幸的是,Scheme 不会接受
(and (eqv? (length x) 1) (eqv? (car x) #t))
x
是一个列表,其第一个元素是计算结果为 #t
或 #f
的 S 表达式(事实上,我只想做 (and (eqv? (length x) 1) (car x))
,但这不起作用)。
谁能解释我做错了什么,或者如何解决它?顺便说一句,有谁知道 ...
在Scheme中的含义(如果有的话)?谢谢!
Hey, I'm trying to use and
in a cond
statement. Basically, instead of simply checking that <exp1>
is true before running some code, I need Scheme to check that <exp1>
AND <exp2>
are true. I understand that (and #t #f)
evaluates to #f
and that (and (= 10 (* 2 5)) #t)
evaluates to #t
. Unfortunately, Scheme will not accept
(and (eqv? (length x) 1) (eqv? (car x) #t))
where x
is a list whose first element is an S-expression that evaluates to either #t
or #f
(in fact, I wanted to just do (and (eqv? (length x) 1) (car x))
, but that didn't work).
Can anyone explain what I am doing wrong, or how to fix it? On a side note, does anyone know what ...
means in Scheme, if anything? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“其中 x 是一个列表,其第一个元素是计算结果为 #t 或 #f 的 S 表达式(事实上,我只想执行 (and (eqv? (length x) 1) (car x)),但是这不起作用。”
在
(car x)
的第二种情况下,您只获取列表中的第一个元素,而不评估它。假设您的列表 x 是一个列表
((eq? 3 3))
,我只是说一些事情。它的长度是 1,如果我们评估它,它的第一个元素的评估结果为#t
>,但在这种情况下(car x)
会检索一个列表(eq? 3 3)
,它是一个由一个符号和两个符号组成的列表 。解决您的问题的方法是使用
eval
,如(eval (car x) (null-environment))
,它评估数据,例如列表 如果您使用((car x))
(如另一个答案中所述),则仅当您的列表的第一个元素是一个 thunk(空值),并且您通过评估列表 < 来构造 x 时,这才有效。 code>(list (lambda () #t))
在这种情况下,您的第一个元素将是一个函数,在不带参数的情况下调用时会产生
#t
。"where x is a list whose first element is an S-expression that evaluates to either #t or #f (in fact, I wanted to just do (and (eqv? (length x) 1) (car x)), but that didn't work."
In the second case of
(car x)
, you just get the first element in the list, you do not evaluate it.Say your list x is a list
((eq? 3 3))
, I'm just saying something. Its length is 1, and it's first element evaluates to#t
if we evaluate it, but(car x)
in this case retrieves a list(eq? 3 3)
, which is a list of one symbol and two numbers.The solution to your problem would be using
eval
, as in(eval (car x) (null-environment))
, which evaluates a datum, such as a list.If you use
((car x))
as noted in another answer, this will only work if your first element of a list is a thunk, a nullary, if you constructed x by evaluating the list(list (lambda () #t))
.In that case, your first element would be a function which when called with no arguments yields
#t
.这有效
(define l1 '(#t #f #t))
但这不是
你想做什么?
如果是第二种,如果可能的话,请考虑在将表达式输入到列表之前对其进行评估。
或者你可以尝试:
This works
(define l1 '(#t #f #t))
But this doesn't
Which are you trying to do?
If the second, then consider evaluating the expression before entering it into the list, if possible.
Or you could try: