使用lazy natural。
Prelude Data.Number.Natural> let xs = [1..10] :: [Natural]
Prelude Data.Number.Natural> :sp xs
xs = _
Prelude Data.Number.Natural> sum xs > 3
True
Prelude Data.Number.Natural> :sp xs
xs = [Data.Number.Natural.S Data.Number.Natural.Z,
Data.Number.Natural.S
(Data.Number.Natural.S Data.Number.Natural.Z),
Data.Number.Natural.S _,_,_,_,_,_,_,_]
要走得懶,用foldr
代替foldl
方式sum
做:
Prelude Data.Number.Natural> let xs = [1..10] :: [Natural]
Prelude Data.Number.Natural> let lazySum = foldr (+) 0
Prelude Data.Number.Natural> lazySum xs > 3
True
Prelude Data.Number.Natural> :sp xs
xs = Data.Number.Natural.S Data.Number.Natural.Z :
Data.Number.Natural.S
(Data.Number.Natural.S Data.Number.Natural.Z) :
Data.Number.Natural.S _ : _
我也嘗試定義與'foldr'總和,但它評估整數的整個列表。我想我應該使用自然,即使'foldr'。 – vis