從this Youtube video:loeb如何在這個haskell示例中終止?
> let loeb fs = xs where xs = fmap ($ xs) fs
> loeb [length, (!! 0)]
[2,2]
這裏的xs
是遞歸定義的,以及如何勒布終止我是無法理解。
從this Youtube video:loeb如何在這個haskell示例中終止?
> let loeb fs = xs where xs = fmap ($ xs) fs
> loeb [length, (!! 0)]
[2,2]
這裏的xs
是遞歸定義的,以及如何勒布終止我是無法理解。
嘗試:
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
的原因是,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
執行懶評價:它是不感興趣的元素的值,所以他們仍然未解時數數。
光榮懶惰評價。 – qed 2015-03-25 09:05:51