python - 使用 map/reduce 添加列表中一对数字的差异

标签 python list functional-programming reduce

我正在使用 Python 练习函数式编程概念。我遇到了这个问题。我已经尝试了很多,但找不到使用函数式编程结构(如 map/reduce、闭包)的解决方案。

问题:给定一个数字列表

list = [10, 9, 8, 7, 6, 5, 4, 3]

使用 Map/Reduce 或任何函数式编程概念,例如

[[10 -9] + [8 - 7] + [6 -5] + [4 - 3]] = 4

对我来说棘手的部分是使用 map/reduce/recursion/closure 隔离对

最佳答案

你要找的递归关系是

f([4, 3, 2, 1]) = 4 - 3 + 2 - 1 = 4 - (3 - 2 + 1) = 4 - f([3, 2, 1])

关于python - 使用 map/reduce 添加列表中一对数字的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33969409/

相关文章:

functional-programming - 将 javascripts native "for loop"与原型(prototype)的 each() 进行比较

scala - Scala 的列表是否在连接运算符下形成了幺半群?

python - 谷歌应用引擎: Difficulty with Users API (or maybe just a Python syntax problem)

python - 为什么 gevent-websocket 是同步的?

java - 从列表构建链接链

list - 查找最大整数的索引

python - 在编译或拟合之前合并 keras 模型?

python - 使用 pandas.DataFrame 进行概率张量乘法

python - 仅当部分单词尚未在列表中时才追加

algorithm - 函数式语言中的等价类和联合/查找