或作为方案中的程序 [英] or as procedure in scheme
问题描述
我想将 or
应用于列表的每个元素,例如:(apply or '(#t #t #f))
预期结果 #t
,但出现错误:
I want to apply or
to every element of list, example:
(apply or '(#t #t #f))
expected result #t
, but I'm getting error:
'#' 到 'apply' 的类型错误(kawa.lang.Macro)(预期:过程、序列或其他运算符)
'#' to 'apply' has wrong type (kawa.lang.Macro) (expected: procedure, sequence, or other operator)
据我所知,or
不是一个程序.有没有什么程序可以代替或
?
As I understand or
is not a procedure.
Is there any procedure that can be used instead of or
?
推荐答案
最简单的方法是使用 exists
*.无论何时使用 (apply or some-list)
,都可以使用 (exists values some-list)
.或者,如果您愿意,您可以定义一个使用 exists
来执行此操作的函数:
The easiest way to do this is with exists
*. Whenever you would use (apply or some-list)
, you can use (exists values some-list)
. Or if you like, you can define a function that uses exists
to do that:
#!r6rs
(import (rnrs base)
(rnrs lists))
(define (or* . lst)
(exists values lst))
values
是恒等函数,所以 (values x)
就是 x
.
values
is the identity function, so (values x)
is just x
.
exists
是一个高阶函数,定义为 (exists f (list x ...))
等价于 (or (fx) ...)
.
exists
is a higher-order function defined so that (exists f (list x ...))
is equivalent to (or (f x) ...)
.
例如 (exists values (list #t #t #f))
等价于 (or (values #t) (values #t) (values #f))
,与 (or #t #t #f)
相同.
For example (exists values (list #t #t #f))
is equivalent to (or (values #t) (values #t) (values #f))
, which is the same as (or #t #t #f)
.
尝试一下:
> (apply or* '(#t #t #f))
#t
> (apply or* '(#f #f #f))
#f
> (or* #t #t #f)
#t
> (or*)
#f
*exists
有时被称为 ormap
或 any
*exists
is sometimes known as ormap
or any
这篇关于或作为方案中的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!