美文网首页
PLAI_Chapter9:Recursion and Cycl

PLAI_Chapter9:Recursion and Cycl

作者: thehgz | 来源:发表于2015-12-13 13:47 被阅读27次

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?
不明觉厉中。。。

相关文章

  • PLAI_Chapter9:Recursion and Cycl

    9.1 Recursive and Cyclic Data Recursion in data can refer...

  • Binary Tree and Recursion

    DFS Non-recursion (use stack) Recursion (自己调用自己)-- Traver...

  • Recursion

    How to calculate the complexity? How about the space?

  • Recursion

    Fibonacci Find the maximum value among array elements Bub...

  • Recursion

    发自简书 递归 导致递归的方法返回而没有再一次进行递归调用,此时我们称为基值情况( base case)。每一个递...

  • Recursion

    有限记忆和无限思维之间的矛盾促生了语言的递归性 语言是用有限手段生成无限话语的装置.如果一种语法没有递归机制,它将...

  • Recursion

    Fibonacci A frog can jump one or two steps at a time. How...

  • recursion

    22 Generate Parentheses 39 Combination Sum 40 Combination...

  • 递归

    recursion完成了iteration,但逻辑清晰,有以下问题: recursion 由stack完成,会溢出...

  • ZXAlgorithm - C5 DFS

    OutlineRecursionCombinationPermutationGraphNon-recursion ...

网友评论

      本文标题:PLAI_Chapter9:Recursion and Cycl

      本文链接:https://www.haomeiwen.com/subject/jptuhttx.html