“对于每个”或“每个”方案中的关键词

发布于 2024-08-10 21:36:56 字数 528 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情绪 2024-08-17 21:36:56

您正在寻找map,尽管您可能想知道Scheme 也有for-eachmap 完全按照 every 的要求进行操作。它对列表中的每个项目执行一些操作,返回一个新的结果列表。

你甚至可以说

(define every map)

You can get your first function bywriting

(define (first symbol)
  (string->symbol (string (string-ref (symbol->string symbol) 0))))

这是坏方案风格,虽然。它看起来就像 60 年代或 70 年代的古老 Lisp,早在字符串出现之前。
无论如何,现在你可以说

(map first '(here comes everybody))
=> (h c e)

for-each 对列表中的每个项目都有某种副作用:

(define initials (map first '(here comes everybody)))
(for-each display initials)
=> hce

You are looking for map, although you probably would like to know that Scheme also has for-each. map does exactly what you want with every. It does something to each item in the list, returning a new list of the results.

You could even say

(define every map)

You can get your first function by writing

(define (first symbol)
  (string->symbol (string (string-ref (symbol->string symbol) 0))))

This 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

(map first '(here comes everybody))
=> (h c e)

for-each does some kind of side effect to each item in the list:

(define initials (map first '(here comes everybody)))
(for-each display initials)
=> hce
不念旧人 2024-08-17 21:36:56

可能是答案你的问题。 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.

镜花水月 2024-08-17 21:36:56

这是对 Nathan 帖子的补充,这是目前对您来说最好的答案...

如果您转向 schemescheme/base 模块语言,您将获得 PLT 的 for 迭代器大军的访问权限。这些看起来有点像其他语言中常见的“foreach”循环。在文档中搜索 for

(define (first symbol)
  (string->symbol (string (string-ref (symbol->string symbol) 0))))

(for/list ([symbol (in-list '(here comes everybody))])
  (first symbol))

=> '(h c e)

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 or scheme/base module languages, you will gain access to PLT's army of for iterators. These look a bit more like the "for each" loops that are common in other languages. Search for for in the docs:

(define (first symbol)
  (string->symbol (string (string-ref (symbol->string symbol) 0))))

(for/list ([symbol (in-list '(here comes everybody))])
  (first symbol))

=> '(h c e)
生活了然无味 2024-08-17 21:36:56

取决于您正在寻找哪个计划。除了上面提到的“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文