如何检查列表是否仅包含#t [英] How to check if a list contains only #t
问题描述
我在球拍和 MIT 方案中尝试使用以下代码,令我惊讶的是编译器抛出错误
I was trying with the following code in racket and MIT scheme, surprise me that the compiler throw err
(foldr and #t '(#t #t #f))
有没有办法使用reduce/fold方式来检查列表是否只包含true或false?我知道 lambda 可以完成这项工作,但它确实让我们想知道为什么这不是一个有效的代码.我记得我可以在 Haskell 中做到这一点.....
Is there any way to use reduce/fold way to check if a list contains only true or false? I know a lambda can do the job, but it really make we wonder why this is not a valid code. I remember I can do it in Haskell.....
TIA.
推荐答案
and
是一个宏,因此它本身没有值.具体来说,它会使评估短路,并且按照您的尝试使用它没有任何意义.因此,Racket 有 andmap
可以在这种情况下使用.(其他实现在不同名称下具有相似的功能——例如,srfi-1 使用 every
.)
and
is a macro, so it doesn't have a value by itself. Specifically, it short-circuits evaluation, and using it as you tried to will not make any sense. For that reason, Racket has andmap
which you can use in such cases. (Other implementations have similar functionality under different names -- for example, srfi-1 uses every
.)
这篇关于如何检查列表是否仅包含#t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!