- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有关于 scheme 和 lisp 的一般性问题。 fold
和 reduce
函数应该如何工作?
在使用 (use-modules (srfi srfi-1))
的 guile 方案中,你可以使用这个:
guile> (fold cons '() '(1 2 3 4))
> (4 3 2 1)
guile> (fold cons '(1 2 3 4) '())
> (1 2 3 4)
我正在使用 JavaScript 在我的 lisp 中处理 fold 函数,我想为 reduce 和 fold 创建一个函数(该函数将返回其中一个函数)。
但是它们应该如何工作?在我的代码中,我正在检查第一个列表是否不为空或者它是否没有结束但是在这里你传递了空列表(第一个代码)。 fold 是否在第二个列表上工作,并检查它是否没有结束或在两个列表上都工作,因为 reverse 也可以工作?当以 '() 作为初始值调用时,fold 执行了多少次,或者它处理整个第一个参数?
这是我的归约函数:
function reduce(fn, list, init = nil) {
if (isEmptyList(list) || isNull(list)) {
return list;
}
let result = init;
let node = list;
if (init === null) {
result = list.car;
node = list.cdr;
}
return (function loop() {
function next(value) {
result = value;
node = node.cdr;
return loop();
}
if (node === nil || !(node instanceof Pair)) {
if (typeof result === 'number') {
return LNumber(result);
}
return result;
}
const item = node.car;
const value = fn(result, item);
if (isPromise(value)) {
return value.then(next);
} else {
return next(value);
}
})();
}
reduce
下面的结果是否正确?
lips> (reduce cons '(1 2 3 4) nil)
((((nil . 1) . 2) . 3) . 4)
lips> (reduce list '(1 2 3 4) nil)
((((nil 1) 2) 3) 4)
lips>
fold 函数在 JavaScript 中应该如何工作? scheme 中fold
和reduce
的具体逻辑是什么?
这是诡计的另一个例子:
guile> (fold-right append '(1 2 3 4) '())
(1 2 3 4)
lips> (reduce append '(1 2 3 4) '())
(1 2 3 4)
它在我的 lisp 中工作相同,这是否意味着我的 reduce 是正确的?如何测试我的函数是否正常工作?
我有一个问题,在 Guile 中:
guile> (fold-right list '(1 2 3 4) '())
> (1 2 3 4)
guile> (fold list '(1 2 3 4) '())
> (1 2 3 4)
但在我的口齿不清中:
lips> (reduce list '(1 2 3 4) '())
((((() 1) 2) 3) 4)
fold-right 实际上是 reduce 吗?因为这个 guile 代码给出了与我的 reduce 相同的结果:
guile> (list (list (list (list '() 1) 2) 3) 4)
> ((((() 1) 2) 3) 4)
最佳答案
https://www.gnu.org/software/guile/manual/html_node/SRFI_002d1-Fold-and-Map.html
方案流程: fold proc init lst1 lst2 …
方案流程: fold-right proc init lst1 lst2 …
将 proc 应用于 lst1 lst2 的元素……以构建结果,并返回该结果。
每个 proc 调用都是 (proc elem1 elem2 … previous),其中 elem1 来自 lst1,elem2 来自 lst2,依此类推。 previous 是上一次调用 proc 的返回,或者是第一次调用的给定 init。 如果任何列表为空,则只返回 init。
fold
从头到尾遍历列表元素。下面显示了一个列表反转及其调用,
(fold cons '() '(1 2 3))
(cons 1 '())
(cons 2 '(1))
(cons 3 '(2 1)
⇒ (3 2 1)
fold-right
从最后到第一个遍历列表元素,即。从右边。因此,例如,下面找到最长的字符串,最后一个字符串中最长的,
(fold-right (lambda (str prev)
(if (> (string-length str) (string-length prev))
str
prev))
""
'("x" "abc" "xyz" "jk"))
⇒ "xyz"
scheme folds 支持多个列表,但我将向您展示如何调整 JavaScript 实现以使其适用于一个列表 -
function reduce (fn, init, list) {
if (isNull(list))
return init
else
return reduce(fn, fn(list.car, init), list.cdr)
}
function reduceRight (fn, init, list) {
if (isNull(list))
return init
else
return fn(list.car, reduceRight(fn, init, list.cdr))
}
由于 JavaScript 支持rest 参数 和spread arguments,支持多个列表非常容易 -
function some (fn, list) {
if (isNull(list))
return false
else
return fn(list.car) || some(fn, list.cdr)
}
function reduce (fn, init, ...lists) {
if (some(isEmpty, lists))
return init
else
return reduce
( fn
, fn (...lists.map(l => l.car), init)
, lists.map(l => l.cdr)
)
}
function reduceRight (fn, init, ...lists) {
if (some(isEmpty, lists))
return init
else
// exercise left for reader
// ...
}
关于javascript - 理解 scheme 中的 fold 和 reduce 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55782227/
(按标题道歉,我不能做得更好) 我的问题是找到一些通用的结构或“标准”函数来执行下一件事: xmap :: (a -> b) -> f a -> g b 然后,我们不仅可以映射元素,还可以映射整个结构
我正在尝试构建一个分为首屏和非首屏部分的页面,让查看者有一种进入海底的印象。我遇到了一个绊脚石,因为当涉及到 CSS 定位的概念时,我的脑子里有些东西就是拒绝点击。 最终,我试图在折叠下方添加另一个部
我正在尝试使用 Rayon启动一系列顶级线程以递归调用模拟函数。该代码在使用 channel 发送和接收时有效,因此它是多线程兼容的,但它无法使用 par_iter() 进行编译。 fn simula
所以我的任务是在 Scheme 中使用 fold-left 或 fold-right 实现最基本版本的 'map' 函数和 'filter' 函数。我很难理解这些函数到底在做什么。这是我所拥有的: (
我的序列、数组等的顺序很重要。我曾尝试在 List、Seq 和 Array 之间进行转换,以查看是否存在差异,并且在每种情况下都将顺序颠倒。 例如,我有一个 [名词] [动词] [形容词] 的序列,它
我发现自己一遍又一遍地重复一个模式,我想把它抽象出来。我相当有信心 coq 具有足够的表现力来捕捉模式,但我在弄清楚如何做到这一点时遇到了一些麻烦。我正在定义一种编程语言,它具有表示句法术语的相互递归
我有一个类型如下的函数: union :: a -> a -> a 和a有 加性 属性(property)。所以我们可以看成union作为 (+) 的一个版本 比如说,我们有 [a] ,并希望执行并行
我编写了以下代码,它创建了一个无限的斐波那契数列: fibs = 1:1:fib 1 1 where fib a b = a+b:fib b (a+b) 上面的代码可以用foldl写吗?或 fol
(rust noob here;我试图理解在高阶函数情况下什么可以/不能/应该/不应该通过引用传递) let a = [1, 2, 3]; 此调用有效: let sum = a.iter().fold
我对 Haskell 基础知识之一有疑问:Fold + 匿名函数 我正在使用 foldl 开发 bin2dec 程序。 解决方案如下所示: bin2dec :: String -> Int bin2d
让我们假设 fn scan(int, int) -> int。 使用时 fn count(x: int, y: int) -> int { scan(x - 1, y - 1) + scan(
我正在尝试实现一个通用的缺点列表,它比本书第 15 章中使用的列表更高级: use std::fmt::Debug; #[derive(Debug)] enum List { Nil,
我有一个网站,顶部有一个水平的 ul/li 按钮。如果有人缩小窗口,按钮会向下折叠成两排。有点丑。 举个例子: http://www.redolog.com 我想知道是否有一个布局指令说“看,你必须至
我正在使用下面的代码,改编自 this线。我能够获取文件夹中的文件列表,但最后出现段错误。知道为什么会这样吗? 有没有办法在for循环中获取当前文件(完整路径)的std::string? boost
问题如何以编程方式确定“折叠”(浏览器显示多少垂直内容)? “折叠”定义为您再也看不到/必须滚动的地方。 我曾尝试使用 JavaScript 来简单地确定浏览器窗口大小来确定折叠;不幸的是 - 这不能
人们普遍认为 1024x768 浏览器是目标,960 - 980 像素的宽度是可以接受的。 (我个人更喜欢 960 的 chrome,但没有争论的意义。) 我的问题是 - 通常可以假设用户的窗口高度是
我正忙于学习 F# 并在玩弄 Seq.fold。任何人都可以解释为什么以下两个调用本质上不相同,一个错误而另一个错误。 这样调用: Seq.fold (fun state input -> state
我相信这是有充分理由的,但我没有看到。 Fold在(说)List返回 the result of applying fold operator op between all the elements
我的家庭作业进行得非常顺利,直到我偶然发现了最后一项任务。 首先,我必须定义一个自定义 List结构体: data List a = Nil | Cons a (List a) deriving Sh
因此,有一种称为“折叠的通用属性”的东西,确切地说如下: g [] = i; g (x:xs) = f x (g xs) g = fold f i 但是,正如您现在可能的那样,有像 dropWhil
我是一名优秀的程序员,十分优秀!