[plt-scheme] Does the procedure know who called it and how manytimes?
On Jul 27, 2007, at 10:09 PM, Majorinc, Kazimir wrote:
> Let us imagine I have large tree data structure of nodes and I want
> to calculate the SIZE of the subtree for some node. I can do it
> easily with some recursive function.
>
> Now, my data structure is not the tree but it is more general
> graph. Using the same recursive procedure, there is a lot of
> redundancy. I can avoid that redundancy if I implement some kind of
> memoization of the SIZE and store values in some hash-table, or
> much better in the special field in the nodes of the graph. In both
> cases, I need to know whether procedure is called by itself (and
> then obey existing memoization) or it is called by some other
> function (and then initiate new memoization.)
>
> There are few ways it can be done, for example, to define two
> procedures, SIZE and SIZE-PRIVATE (called only from SIZE), but it
> is certainly less elegant approach.
Why not
(define (size g)
(define (local-size g)
... (map local-size (neighbors current-node)) ...)
(local-size g))
Now you know that size is a global call and local-size is a local call.
-- Matthias