Haskell 无限递归

问题描述 投票:0回答:2

以下函数计算斐波那契数列:

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

如果我们运行它,我们将得到一个无限列表,但是递归是如何工作的呢?如果函数不断调用自身,为什么它会在屏幕上打印数字?如果您能解释编译器如何管理调用,我将不胜感激。

list haskell recursion fibonacci infinite
2个回答
11
投票

我画了一张图,也许对你有帮助。
请注意

zipWith op (x:xs) (y:xs) = (op x y):zipWith xs ys
,这就是
zipWith
沿着列表向右“移动”的方式。它正在读取元素并输出总和:


这是更详细的分步评估。 (虽然我会粘贴那里的内容的副本,但内存中只有一份副本。)我会使用

....
来表示我懒得写出来的东西。

fib = 0:1:zipWith (+) fib (tail fib)
    = 0:1:zipWith (+) (0:1: .... ) (tail (0:1: .... )
    = 0:1:(0+1:zipWith (+) (1:(0+1: .... )) ( 0+1:..... ))
    = 0:1:1:zipWith (+) (1: ....) (......)

请注意,现在我们知道了

zipWith (+) fib (tail fib) = 1:.....

    = 0:1:1:zipWith (+) (1:1: ....) (1:......)
    = 0:1:1:(1+1):zipWith (+) (1:(1+1): .....) ((1+1):....)
    = 0:1:1:2:zipWith (+) (1:2: .....) (2:....)

我会走快一点:

    = 0:1:1:2:(1+2):zipWith (+) (2: .....) (....)
    = 0:1:1:2:3     :zipWith (+) (2:3 .....) (3:....)
    = 0:1:1:2:3:(2+3):zipWith (+) (3:(2+3):.....) ((2+3):.....)
    = 0:1:1:2:3:5     :zipWith (+) (3:5:.....) (5:.....)
    = 0:1:1:2:3:5:8    :zipWith (+) (5:8:....) (8:......)
    = 0:1:1:2:3:5:8:13  :zipWith (+) (8:13:....) (13:......)
    = 0:1:1:2:3:5:8:13:21:zipWith (+) (13:21....) (21:......)

在每个阶段,

zipWith
函数的最后两个参数就像指向
fib
列表中比目前更靠前的(一个和两个位置)的指针。


3
投票

一句话:懒惰。 Haskell 中的列表更像是一个生成器:它只会在其他东西需要时才计算值。

例如

head [1 , 2+3]
不会执行加法,因为不需要。类似地,如果我们递归地让
ones = 1 : ones
,那么
head ones = head (1 : ones) = 1
不需要评估所有的尾部。

你可以尝试猜测如果我们打印一对

x
会发生什么,定义如下:

x = (n, fst x + 1)

上面我们使用(惰性)对而不是(惰性)列表,但推理是相同的。除非其他东西需要,否则不要评估任何东西。

© www.soinside.com 2019 - 2024. All rights reserved.