D 中的无限数据结构
我在 D http://www.digitalmars.com/d/2.0 中找到了函数参数惰性求值的示例/lazy-evaluation.html
我想知道如何在 D 中实现可能的无限数据结构,就像 haskell 列表的常见行为一样。
有一些例子吗?
无限斐波那契数列等价于什么:
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
I found examples of lazy evaluation of function arguments in D http://www.digitalmars.com/d/2.0/lazy-evaluation.html
I´m wondering how to implement possible infinite Datastructures in D like it´s common behaviour of haskell´s lists.
Are there some Examples ?
What is the equivalent of the infinite fibonacci sequence:
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Arlen 在评论中提到 D 版本很快就会溢出,因为它不使用 bigint。幸运的是,bigint 可以作为库模块使用,并且与
recurrence
兼容:Arlen mentioned in a comment that the D version quickly overflows, because it doesn't use bigints. Fortunately, bigints are available as a library module, and are compatible with
recurrence
:这基本上与 Mehrdad 的答案相同,但在我看来,使用了稍微更具可读性的语法:
This is basically the same thing as Mehrdad's answer but uses, in my opinion, slightly more readable syntax:
棘轮怪人涵盖了斐波那契。
因为它是作为值类型实现的,所以获取它的副本将按预期运行。这也适用于任何大小的任何“数据结构”(因为OP正在使用它,而不是结构),其中有限的存储量和转换操作可以定义从任何点可达的域。
ratchet freak covered Fib.
Because it is implemented as a value type, taking copies of it will act as expected. This will also work for any "data structure" (as the OP was using it, not a struct) of any size where a finite amount of storage and a transition operation can define the reachable domain from any point.
查看示例中如何实现随机 https:// github.com/D-Programming-Language/phobos/blob/master/std/random.d
但这是斐波那契数列
check out how randoms are implemented for an example https://github.com/D-Programming-Language/phobos/blob/master/std/random.d
but here's the fibonacci sequence