Assessment of the normal order and applicative order in the diagram

For the first example given on the site: View-Site , I understand that the normal order is evaluated [6;1;1] , and the estimated order is rated [6;2;2]

Can anyone confirm my rating?

Respectfully,
Darkie

+4
source share
1 answer

Ok, let's take a look at the assessment steps (cons res v) with a normal order estimate:

v defined as (cons a (cons b '())) , so we have cons res (cons a (cons b '())) . res defined as (foo ...) , so we have

 (cons (foo (begin (set! a (+ a 1)) a) (begin (set! b (* b 2)) b)) (cons a (cons b '()))) 

Finally, foo xy defined as (+ xyy) , therefore, replacing (begin (set! a (+ a 1)) a) with x and (begin (set! b (* b 2)) b) with y , we get:

 (cons (+ (begin (set! a (+ a 1)) a) (begin (set! b (* b 2)) b) (begin (set! b (* b 2)) b)) (cons a (cons b '()))) 

So, now let's evaluate this: to get the result of the minuses, you first need to evaluate its first argument, (+ ...) . Therefore, we first need to evaluate the first argument + , which is (begin (set! a (+ a 1)) a) . This evaluates to 2, so the value of a now 2, and the first argument + also equal to 2. Now we do the same with the second argument. This also evaluates to 2 and sets b to 2. The third argument is again (begin (set! b (* b 2)) b) , so the value of b now 4, and the third argument is 4. So the first argument to cons is the result (+ 2 2 4) , which is 8, and the values a and b are 2 and 4.

Now we need to evaluate the second argument (cons a (cons b '())) . Since the values โ€‹โ€‹of a and b are 2 and 4, the final result is (8 2 4) .

+3
source

Source: https://habr.com/ru/post/1313536/


All Articles