loeb如何在这个haskell示例中终止?

时间:2015-03-25 08:43:57

标签: haskell

来自this Youtube video

> let loeb fs = xs where xs = fmap ($ xs) fs
> loeb [length, (!! 0)]
[2,2]

这里的xs是递归定义的,而loeb终止的方式超出了我的范围。

2 个答案:

答案 0 :(得分:8)

试一试:

loeb [length, (!! 0)]
= xs where xs = fmap ($ xs) [length, (!! 0)]
= xs where xs = [length xs, xs !! 0]

当然length xs只是2,所以xslength 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执行延迟评估:它对元素的值不感兴趣,因此在计数时它们仍然是未解决的