> let loeb fs = xs where xs = fmap ($ xs) fs
> loeb [length, (!! 0)]
[2,2]
这里的xs
是递归定义的,而loeb终止的方式超出了我的范围。
答案 0 :(得分:8)
试一试:
loeb [length, (!! 0)]
= xs where xs = fmap ($ xs) [length, (!! 0)]
= xs where xs = [length xs, xs !! 0]
当然length xs
只是2,所以xs
(length xs
)的第一个元素也是2。
记住:length xs
不需要评估列表中的项目:
length [undefined, undefined] = 2
答案 1 :(得分:6)
原因是length
不评估列表的元素。它只是计算元素。 length
(语义等价)定义为:
length (_:xs) = 1 + length xs
length _ = 0
即使其中一个元素因此计算出导致无限循环的表达式,这也无关紧要。只要列表本身当然不是无限的。
现在您致电loeb [length, (!! 0)]
,它将被评估为:
loeb [length, (!! 0)]
xs = fmap ($ xs) [length,(!! 0)]
xs = [length xs,xs !! 0]
因此length xs
执行延迟评估:它对元素的值不感兴趣,因此在计数时它们仍然是未解决的。