或按计划中的程序 [英] or as procedure in scheme

查看:69
本文介绍了或按计划中的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将或应用于列表的每个元素,例如:(应用或'(#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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆