使用“和”在方案中

发布于 2024-09-09 00:48:20 字数 617 浏览 6 评论 0原文

嘿,我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟织青萝梦 2024-09-16 00:48:20

“其中 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.

累赘 2024-09-16 00:48:20

这有效
(define l1 '(#t #f #t))

(car l1)
; #t    
(and (car l1) (car (cdr l1)))
; #f

但这不是

(define l2 '((eq? 1 1) (eq? 1 2)))

(car l1)
; '(eq? 1 1)  
(and (car l1) (car (cdr l1)))
; fails

你想做什么?

如果是第二种,如果可能的话,请考虑在将表达式输入到列表之前对其进行评估。

或者你可以尝试:

(define l3 (list eq? 1 1))

l3
; '(#<procedure:eq?> 1 1)

(apply (car l3) (cdr l3))
; #t

This works
(define l1 '(#t #f #t))

(car l1)
; #t    
(and (car l1) (car (cdr l1)))
; #f

But this doesn't

(define l2 '((eq? 1 1) (eq? 1 2)))

(car l1)
; '(eq? 1 1)  
(and (car l1) (car (cdr l1)))
; fails

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:

(define l3 (list eq? 1 1))

l3
; '(#<procedure:eq?> 1 1)

(apply (car l3) (cdr l3))
; #t
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文