且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Haskell:列表中每第二个元素加倍

更新时间:2021-07-11 22:55:47

这还不错,以建议的修复为模.一旦您对基础库更加熟悉,您可能会避免显式递归而支持一些更高级别的函数,例如,您可以创建一个函数列表,其中每个函数都是 *2 并应用(zip) 将该函数列表添加到您的数字列表中:

That's not bad, modulo the fixes suggested. Once you get more familiar with the base library you'll likely avoid explicit recursion in favor of some higher level functions, for example, you could create a list of functions where every other one is *2 and apply (zip) that list of functions to your list of numbers:

double = zipWith ($) (cycle [id,(*2)])