How does Scheme rate a man or boy test?

This is a man or a boy . Circuit Code:

(define (A k x1 x2 x3 x4 x5)
  (define (B)
    (set! k (- k 1))
    (A k B x1 x2 x3 x4))
  (if (<= k 0)
      (+ (x4) (x5))
      (B)))

To simplify the assessment process, I rewrite it like this:

(define (A k x1 x2)
  (define (B)
    (set! k (+ k -1))
    (A k B x1))
  (if (> 1 k)
      (x2)
      (B)))

I can not understand why it (A 2 (lambda () 1) (lambda () -1))returns 1.

Can anyone explain how the schema interpreter evaluates this expression step by step. If you can attach environment diagrams, all the better :)

+4
source share
2 answers

The question is very subtle, and at the first moment I thought that the call would cause the infinte loop. But the real thing is this:

Let the calls F1 and F2 begin, two functions passed to A for the first time, i.e.

F1 = (lambda() 1)
F2 = (lambda() -1)

, (A 2 F1 F2), A , E1:

Environment E1

, A B1. B1 k E1, A, 1, x1, F1. , , : (A 1 B1 F1). , (E2), :

enter image description here

- , A B2, k E2, A 0, x1 ( B1). , (A 0 B2 B1), :

enter image description here

, A x2, B1. B1 k ( E1), A 0, x1, E1 F1. , (A 0 B1 F1) , , :

enter image description here

, , A x2, F1, 1. !

+8

Stepper DrRacket. Stepper . .

, as-is . , , , .

(define (A k x1 x2)
  (local [(define (B _) (A (- k 1) B x1))]
    (if (< k 2) (x2 '_) (B '_))))

(A 2 (lambda (_) 1) (lambda (_) -1))

:

(define (A k x1 x2)
  (define (B _) (A (- k 1) B x1))
  (if (< k 2) (x2 '_) (B '_)))

local . _ B. . , .

, :

enter image description here

+2

All Articles