Search code examples
schemelispsicp

What (define (p) (p)) does?


I'm reading the "Structure and interpretation of computer programs" 2nd edition in the exercise 1.5, I found a combination that I didn't understand what it does exactly (define (p) (p)).

When I called the procedure (p) I had the cursor blinking in the next line without the ability to write anything .

(define (p) (p))
(p)

I don't know what to expect from this procedure because I defined it by itself.


Solution

  • p is a procedure with no parameters. Its body is (p). In Scheme, we call procedures by surrounding them in brackets together with their arguments. Given that p doesn't have parameters, (p) simply calls p. Which calls p. Which calls p... and so on. So what does it do? an infinite loop! and that is all.