nandin
nandin

Reputation: 2575

What is the meaning of this Scheme

I am new to Scheme, by looking at the Exercise 1.5 of SICP, what is the meaning/usage of this expression?

(define (p) (p))

Thanks!

Upvotes: 5

Views: 338

Answers (2)

bandi
bandi

Reputation: 4254

'define' is defined in the very beginning, in chapter 1:

The general form of a procedure definition is

(define (< name> < formal parameters>) < body>)

After you evaluate the definition you see that your procedure is simply calling itself. The trick lies in the evaluation order of the arguments of the 'test' procedure, as you could figure out from the question of the exercise.

Upvotes: 2

Nick Dandoulakis
Nick Dandoulakis

Reputation: 43140

(define (p) (p))

The above defines a function p that takes no arguments and calls itself recursively (infinitely).

The exercise 1.5 is about applicative-order vs normal-order evaluation.

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

(test 0 (p))

In applicative-order all arguments are evaluated and then they are applied to the test, so the program will freeze if the interpreter uses that kind of evaluation in this specific case.

Upvotes: 8

Related Questions