3

This is the code:

(define (p) (p))
(define (test x y) 
     (if    (= x 0) 
          0 
          y))
(test 0 (p))

What I think(not sure) is that for applicative-order interpreter, (p) procedure is evaluated first which results in an infinite loop.

While for the normal-order interpreter, test procedure is evaluated, which results in test procedure returning 0. So p procedure will not be evaluated.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
user3450695
  • 2,281
  • 4
  • 16
  • 16
  • This has exactly the same code as [seek for some explanation on SICP exercise 1.5](http://stackoverflow.com/q/16036139/1281433) which, in turn, is from the [SICP book](http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-10.html#%_thm_1.5). You really shouldn't copy and paste other people's code without attribution. – Joshua Taylor Oct 03 '14 at 20:29
  • This question was given by my professor as an exercise. So, I didn't know it was from a book or on this amazing site. I'm sorry if my actions offended anyone – user3450695 Oct 03 '14 at 21:05
  • 1
    Understood. Your professor also shouldn't copy material from textbooks without attribution. – Joshua Taylor Oct 03 '14 at 21:09

1 Answers1

2

Your assumptions are right, evaluation occurs just as predicted. In an applicative-order evaluator the parameters to a function call are evaluated before binding them to the procedure's parameters - so the argument (p) will result in an infinite loop, and we'll never enter test's body.

On the other hand, a normal-order interpreter delays evaluation until the last moment, and the (p) invocation will not be evaluated until needed - in other words, (test 0 (p)) will not result in an infinite loop because this execution path never uses the y parameter (which was bound to (p)), whereas (test 1 (p)) will loop forever because we manage to reach the point where y is being used to produce a value.

Testing this is easy for the applicative-order interpreter - that's how standard Scheme works. For testing the normal-order evaluation, you can use an special interpreter with lazy evaluation, like the one implemented in chapter 4 of SICP.

Óscar López
  • 232,561
  • 37
  • 312
  • 386