Haskell Phase Reduction Generation

Is there a system that will generate a stepwise reduction of a Haskell expression? The goal is to provide a case study of lazy assessment. It would be nice if it were written in Haskell, but this is not a requirement.

For example, suppose we have:

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)

The score > myTake 3 $ myCycle [1, 2]should show a sequence like this. (Please correct me if I am wrong about this.)

>  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]

The purpose of all this is to illustrate how lazy pricing makes endless lists possible.

+4
source share

Source: https://habr.com/ru/post/1687955/


All Articles