你将如何在 Haskell 中(重新)实现迭代?
iterate :: (a -> a) -> a -> [a]
(您可能知道)iterate
是一个接受函数和起始值的函数。然后它将函数应用于起始值,然后将相同的函数应用于最后的结果,依此类推。
Prelude> take 5 $ iterate (^2) 2
[2,4,16,256,65536]
Prelude>
结果是一个无限列表。 (这就是我使用 take
的原因)。 我的问题是,您如何在 Haskell 中实现自己的 iterate'
函数,仅使用基础知识 ((:)
(++)
lambdas、pattern匹配、守卫等)?
(哈斯克尔初学者在这里)
iterate :: (a -> a) -> a -> [a]
(As you probably know) iterate
is a function that takes a function and starting value. Then it applies the function to the starting value, then it applies the same function to the last result, and so on.
Prelude> take 5 $ iterate (^2) 2
[2,4,16,256,65536]
Prelude>
The result is an infinite list. (that's why I use take
).
My question how would you implement your own iterate'
function in Haskell, using only the basics ((:)
(++)
lambdas, pattern mataching, guards, etc.) ?
(Haskell beginner here)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,iterate 构造了一个由 a 递增的无限值列表 f。因此,我首先编写一个函数,将一些值 a 添加到通过使用 f a 递归调用 iterate 构建的列表中:
由于惰性求值,仅构建列表的该部分将评估计算我的函数值所必需的。
Well, iterate constructs an infinite list of values a incremented by f. So I would start by writing a function that prepended some value a to the list constructed by recursively calling iterate with f a:
Thanks to lazy evaluation, only that portion of the constructed list necessary to compute the value of my function will be evaluated.
另请注意,您可以在报告的 Standard Prelude< 中找到基本 Haskell 函数范围的简明定义/a>.
阅读这个简单的定义列表,这些定义本质上是从原始基元中引导出一个丰富的库,在提供“haskell 方式”的窗口方面非常有教育意义,并且令人大开眼界。
我记得很早的时候读到的一个顿悟时刻:
data Bool = False |确实如此。
Also note that you can find concise definitions for the range of basic Haskell functions in the report's Standard Prelude.
Reading through this list of straightforward definitions that essentially bootstrap a rich library out of raw primitives can be very educational and eye-opening in terms of providing a window onto the "haskell way".
I remember a very early aha moment on reading:
data Bool = False | True
.您已经获得了它的最简单的实现;然而,还有其他方法。
一种方法是使用高阶函数,如下所示:
函数
unfoldr
是一个用于创建列表的高阶函数。这是它最直接的实现:与
foldr
抽象减少列表的直接递归的方式相同,unfoldr
抽象创建它们的直接递归。You've already been given the simplest possible implementation of it; however, there are other ways.
One way is to use a higher order function, like so:
The function
unfoldr
is a higher-order function meant for creating lists. Here's the most straightforward implementation of it:In the same way that
foldr
abstracts of the direct recursion of reducing lists,unfoldr
abstracts over the direct recursion of creating them.