module - 在方案中向右折叠

标签 module scheme racket

我使用 DrRacket 作为方案编译器。当我尝试使用右折叠功能时,我得到“右折叠:模块中的未绑定(bind)标识符:右折叠”。在调用这个函数之前我需要包含一些库吗?

谢谢!

最佳答案

我认为您正在寻找foldrfoldr是右折叠的 Racket 实现。

来自文档:

Like map, foldl applies a procedure to the elements of one or more lists. Whereas map combines the return values into a list, foldl combines the return values in an arbitrary way that is determined by proc.

[…]

[foldr is ] like foldl, but the lists are traversed from right to left. Unlike foldl, foldr processes the lsts in space proportional to the length of lsts (plus the space for each call to proc).

编辑:

您可能还想按照 Chris 的建议使用 srfi/1 库。 ,特别是如果您希望向右折叠像 srfi/1 recommendation 那样工作。有一些细微的差别:

#lang racket
(require srfi/1)
(foldr list* '() '(a b c) '(1 2 3))
(fold-right list* '() '(a b c) '(1 2 3 4))
(foldr list* '() '(a b c) '(1 2 3 4))

输出:

Welcome to DrRacket, version 5.3 [3m].
Language: racket; memory limit: 128 MB.
'{a 1 b 2 c 3}
'{a 1 b 2 c 3}
[ERROR] foldr: given list does not have the same size as the first list: '{1 2 3 4}

注意:fold-right 在不同大小的列表上不会出错,但您仍然只能从第二个列表中获得 1、2、3 个原子。这符合 srfi/1 规范:

The fold operation terminates when the shortest list runs out of values

foldr 出现错误。

关于module - 在方案中向右折叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12530206/

相关文章:

functional-programming - (Racket) 返回满足条件的列表的子列表

scheme - 方案中的笛卡尔积

list - 绝对第一次使用scheme,简单的列表操作

binary - Racket 二进制加法

vue.js - Vue 找不到模块图片位置

ruby - ruby 中 A::B 类名的含义是什么?

perl - 事件状态 Perl : Unable to download modules using ppm

scheme - foldl 和 foldr 如何工作,在一个例子中分解?

scheme - 帮助动态风和调用/抄送

javascript - 如果一个JS 6模块被导入到一个页面的多个模块中,那么它的 "main line"代码是否只执行一次?