PLAI_Chapter9:Recursion and Cycles: Procedures and Data

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:

  1. The ability to create compound structures (such as nodes that have references to
    children).
  2. 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:

  1. Make sure the value is sufficiently obscure so that it can never be used in a meaningful context.
  2. 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.
  3. 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?
不明觉厉中。。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,811评论 0 23
  • 一直说生活的目的就是快乐,没有质疑过这个生活目的。现在忽然有点疑问,快乐确实让人轻松愉快,但是痛苦无疑更让人成长。...
    Stupidity阅读 99评论 0 0
  • 晚上,和爸爸一起去看玉奇打篮球,他和小学组打完没有走又跑到中学组。因为爸爸给他灌输了要跑位的意识,他不在像以前一样...
    小溪_简书阅读 207评论 0 0