data Tree a
= Leaf a |
Node a (Tree a) (Tree a)
flatten :: Tree a -> [a]
flatten (Leaf x) = [x]
flatten (Node x s t)
= x : flatten s ++ flatten t
|
data Tree a
= Leaf { val::a, flatten::[a] } |
Node { val::a, left,right::(Tree a), flatten::[a] }
leaf x = Leaf x [x]
node x l r = Node x l r (x : (flatten l ++ flatten r))
|
General comment:
Memoisation can be problematic in a lazy language; in order to ensure prompt computation of the memoised value, it is possible to make the constructors strict in their final arguemnts.
Left to right comment:
This refactoring memoises the computation of
|
Right to left comment:
In this direction the refactoring removes the memoisation, and replaces it with a separate computation of the value of |
Left to right conditions:
The definition of the function to be memoised needs to be primitive recursive: the value of the function on |
Right to left conditions:
The memoised code needs to have arisen through a transformation such as this. |