PLAI_Chapter9:Recursion and Cycl
9.1 Recursive and Cyclic Data
Recursion in data can refer to one of two things. It can mean referring to something of the same kind, or referring to the same thing itself.
Adding recursive data, such as lists and trees, to our language is quite straightforward. We mainly require two things:
- The ability to create compound structures (such as nodes that have references to
children). - The ability to bottom-out the recursion (such as leaves).
Absent some magical Racket construct we haven’t yet seen, it becomes clear that we can’t create a cyclic datum in one shot. Instead, we need to first create a “place” for the datum, then refer to that place within itself. The use of“then”—i.e., the introduction of time—should suggest a mutation operation. Indeed, let’s try it with boxes.
Note that this program will not run in Typed PLAI as written. We’ll return to typing such programs later [REF]. For now, run it in the untyped (#lang plai) language.
(let ([b (box 'dummy)])
(begin
(set-box! b b)
b))
9.2 Recursive Functions
In a shift in terminology, a recursive function is not a reference to a same kind of function but rather to the same function itself.
We again have to follow a three-step process: first
create a placeholder, then refer to the placeholder where we want the cyclic reference,
and finally mutate the placeholder before use. Thus:
(let ([fact (box 'dummy)])
(let ([fact-fun
(lambda (n)
(if (zero? n)
1
(* n ((unbox fact) (- n 1)))))])
(begin
(set-box! fact fact-fun)
((unbox fact) 10))))
In fact, we don’t even need fact-fun: I’ve used that binding just for clarity. Observe
that because it isn’t recursive, and we have identifiers rather than variables, its use can
simply be substituted with its value:
(let ([fact (box 'dummy)])
(begin
(set-box! fact
(lambda (n)
(if (zero? n)
1
(* n ((unbox fact) (- n 1))))))
((unbox fact) 10)))
There is the small nuisance of having to repeatedly unbox fact. In a language with
variables, this would be even more seamless:
(let ([fact 'dummy])
(begin
(set! fact
(lambda (n)
(if (zero? n)
1
(* n (fact (- n 1))))))
(fact 10)))
Our preceding discussion of this pattern shows a clear temporal sequencing: create,
update, use. We can capture it in a desugaring rule. Suppose we add the following new
syntax:
(rec name value body)
As an example of its use,
(rec fact
(lambda (n) (if (= n 0) 1 (* n (fact (- n 1)))))
(fact 10))
desugar to this
(let ([name 'dummy])
(begin
(set! name value)
body))
This naturally inspires a question: what if we get these out of order? Most interestingly, what if we try to use name before we’re done updating its true value into place?
(letrec ([x x])
x)
this leaks the initial value given to the placeholder—a value that was never meant for public consumption. If a developer accesses and uses it inadvertently, however, they are effectively computing
with nonsense.
There are generally three solutions to this problem:
- Make sure the value is sufficiently obscure so that it can never be used in a meaningful context.
- Explicitly check every use of an identifier for belonging to this special “premature” value. While this is technically feasible, it imposes an enormous performance penalty on a program. Thus, it is usually only employed in teaching
languages. - Allow the recursion constructor to be used only in the case of binding functions,
and then make sure that the right-hand side of the binding is syntactically a function. Unfortunately, this solution can be a bit drastic because it precludes writing,for instance, structures to create graphs.
9.4 Without Explicit State
Y Combinator ......
(define Y
(lambda (le)
((lambda (f) (f f))
(lambda (f)
(le (lambda (x) ((f f) x)))))))
Exercise
Does the above solution use state anywhere? Implicitly?
不明觉厉中。。。