gpt4 book ai didi

d - D 中的无限数据结构

转载 作者:行者123 更新时间:2023-12-04 00:01:50 30 4
gpt4 key购买 nike

我在 D http://www.digitalmars.com/d/2.0/lazy-evaluation.html 中找到了函数参数的惰性求值示例

我想知道如何在 D 中实现可能的无限数据结构,就像 Haskell 列表的常见行为一样。

有一些例子吗?

无限斐波那契数列的等价物是什么:

fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

最佳答案

查看示例中如何实现随机数 https://github.com/D-Programming-Language/phobos/blob/master/std/random.d

但这是斐波那契数列

struct FiboRange{
enum bool empty=false;//infinite range

long prev=0,curr=1;//the state for next calculations

@property long front(){
return curr;//current value
}

void popFront(){//calculate the next value
long tmp = curr;
curr += prev;
prev = tmp;
}

}

关于d - D 中的无限数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6512574/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com