是否有一个系统可以逐步减少Haskell表达式?目的是提供延迟评估的教程示例。如果它是用Haskell编写的,那会很好,但这不是必需的。
例如,假设我们有:
myCycle xs = xs `myAppend` myCycle xs
[] `myAppend` ys = ys
(x:xs) `myAppend` ys = x: (xs `myAppend` ys)
myTake 0 _ = []
myTake _ [] = []
myTake n (x:xs) = x: (myTake (n-1) xs)
> myTake 3 $ myCycle [1, 2]
的评估应该显示这样的序列。 (如果我在这方面犯了任何错误,请纠正我。)
> myTake 3 $ myCycle [1, 2]
=> myTake 3 ([1, 2] `myAppend` myCycle [1, 2])
=> myTake 3 (1:([2] `myAppend` myCycle [1, 2]))
=> 1: myTake 2 ([2] `myAppend` myCycle [1, 2]) -- Not showing 3-1 => 2
=> 1: myTake 2 (2:([] `myAppend` myCycle [1, 2]))
=> 1:2: myTake 1 ([] `myAppend` myCycle [1, 2])
=> 1:2: myTake 1 (myCycle [1, 2])
=> 1:2: myTake 1 ([1, 2] `myAppend` myCycle [1, 2])
=> 1:2: myTake 1 (1:([2] `myAppend` myCycle [1, 2]))
=> 1:2:1: myTake 0 ([2] `myAppend` myCycle [1, 2])
=> 1:2:1:[] = [1, 2, 1]
所有这一切的要点是说明懒惰的评估如何使无限列表成为可能。