在 Chez Scheme 中显示乱序

display working out of order in Chez Scheme

我在 Mac 上使用 chez 9.5.4。

以下代码:

;; demo.ss
(map display (list "this " "is " "weird "))

这样做:

$ chez --script demo.ss
weird this is 

为什么意外的尤达?

我该如何防止这种情况发生?

它在 Chicken Scheme 中按预期工作。

As answered by u/bjoli on reddit:

Your code is relying on unspecified behaviour: The order of map is unspecified.

You want for-each.

Chez has no stack overflow (like racket or guile). Doing a right fold with map means no (reverse ...) At the end. It is faster, except in some continuation-heavy code.

Schemes without the expanding stack optimization all do a left fold. Like chicken.

简而言之,这正是 map 所做的。

根据 r7rs-small 规范,https://small.r7rs.org/attachment/r7rs.pdf 第 51 页:

The dynamic order in which proc is applied to the elements of the list s is unspecified.

这是因为 map 旨在通过对每个元素应用纯函数来转换列表。 map 的唯一作用应该是它的结果列表。

正如 divs1210 引用 u/bjoli 指出的那样,Scheme 还定义了一个过程来完成您想要的事情。事实上,for-each 在 r7rs-small pdf 的同一页上有描述!它说:

The arguments to for-each are like the arguments to map, but for-each calls proc for its side effects rather than for its values. Unlike map, for-each is guaranteed to call proc on the elements of the list s in order from the first ele- ment(s) to the last, and the value returned by for-each is unspecified.