“对于每个”或“每个”方案中的关键词
Scheme 中是否有 for
循环或 for every
循环?
我一直在搜索,发现有一个关键字“every
”,但我使用的方案编译器语言没有预先构建此函数。这就是它应该做的,它可以在此处找到
(define (first-letters sent)
(every first sent))
> (first-letters '(here comes the sun))
(H C T S)
我如何重写每个
功能?使用其他预定义函数。我使用的语言位于 DrScheme - 编程语言要点(第 3 版)中,
我尝试了 DrScheme 中所有预装的编译器,但没有一个可以编译 every
函数。
有什么想法吗?
Is there a for
loop or for each
loop in Scheme ?
I've been searching around and found there is a keyword "every
" but the scheme compiler language I'm using does not have this function pre-build in. This is what it suppose to do, it can be find here
(define (first-letters sent)
(every first sent))
> (first-letters '(here comes the sun))
(H C T S)
How can I re-write the every
function ? using other pre-defined function. The language I'm using is in the DrScheme - Essentials of Programming Languages (3rd ed)
I tried all the pre-installed compiler in DrScheme none of them can compile the every
function.
Any ideas ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在寻找
map
,尽管您可能想知道Scheme 也有for-each
。map
完全按照every
的要求进行操作。它对列表中的每个项目执行一些操作,返回一个新的结果列表。你甚至可以说
You can get your
first
function bywriting这是坏方案风格,虽然。它看起来就像 60 年代或 70 年代的古老 Lisp,早在字符串出现之前。
无论如何,现在你可以说
for-each
对列表中的每个项目都有某种副作用:You are looking for
map
, although you probably would like to know that Scheme also hasfor-each
.map
does exactly what you want withevery
. It does something to each item in the list, returning a new list of the results.You could even say
You can get your
first
function by writingThis is bad Scheme style, though. It looks like ancient Lisp from the 60s or 70s, back before strings were in the language.
Anyway, Now you can say
for-each
does some kind of side effect to each item in the list:这可能是答案你的问题。 Map 函数,以函数和列表(-s)作为参数,将函数应用于列表的元素,返回结果。
This could be the answer of your question. Map function, takes a function and list(-s) as arguments, applies function to elements of list, returns the results.
这是对 Nathan 帖子的补充,这是目前对您来说最好的答案...
如果您转向
scheme
或scheme/base
模块语言,您将获得 PLT 的for
迭代器大军的访问权限。这些看起来有点像其他语言中常见的“foreach”循环。在文档中搜索for
:This is an addition to Nathan's post, which is the best answer for you right now...
If you ever move over to the
scheme
orscheme/base
module languages, you will gain access to PLT's army offor
iterators. These look a bit more like the "for each" loops that are common in other languages. Search forfor
in the docs:取决于您正在寻找哪个计划。除了上面提到的“for-each”和“map”(它们是各种标准的一部分,因此存在于所有实际方案中)之外,您还可以找到特定于实现的扩展。例如,PLT 方案有大量此类形式,您可以在此处阅读< /a>.
Depends which Scheme you're looking at. Apart from "for-each" and "map" mentioned above (which are part of the various standards, hence present in all real Schemes), you can find impelementation-specific extensions. For example, PLT scheme has a whole slew of such forms, which you can read about here.