开发者

What is the meaning of this Scheme

开发者 https://www.devze.com 2023-01-01 12:26 出处:网络
I am new to Scheme, by looking at the Exercise 1.5 of SICP, what is the meaning/usage开发者_如何学C of this expression?

I am new to Scheme, by looking at the Exercise 1.5 of SICP, what is the meaning/usage开发者_如何学C of this expression?

(define (p) (p))

Thanks!


(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.


'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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号