或按计划中的程序 [英] or as procedure in scheme
问题描述
我想将或应用于列表的每个元素,例如:
(应用或'(#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:
从#"到应用"的类型错误(kawa.lang.Macro)(预期:过程,序列或其他运算符)
'#' to 'apply' has wrong type (kawa.lang.Macro) (expected: procedure, sequence, or other operator)
据我了解,或不是一个过程.是否可以使用任何程序代替
或?
As I understand or
is not a procedure.
Is there any procedure that can be used instead of or
?
推荐答案
最简单的方法是使用 exists
*.每当您使用(应用列表或某些列表)
时,都可以使用(存在某些列表的值)
.或者,如果您愿意,可以定义一个使用 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) ...)
.
例如(存在值(list #t #t #f))
等效于(或(values #t)(values #t)(values #f))
,与(或#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
这篇关于或按计划中的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!