1
votes

Could someone please explain what happens once the continuation is called for this.

    ((cdr (or (call/cc (lambda (cc) (cons 2 (lambda () (cc #f))))) (cons 3 5))))

    ((cdr (or (call/cc (lambda (cc) (cons 2 (lambda () (cc #f))))) (cons 3 (lambda() (+ 3 2)))))) 

The first statement gives error but the second one returns 5. My question is why is call/cc searching for a procedure like the second statement and not output 5 directly.

1

1 Answers

1
votes

In ((cdr X)) you will get an error, if X doesn't evaluate to a pair where the cdr is a thunk.

In your first expression, the initial value of X is (cons 2 (lambda () (cc #f))). So everything is fine. However when you invoke the thunk, the expression (cc #f) will return #f to the or, and thus (or #f (cons 3 5)) will evaluate to a pair with a 5 in the cdr. We now have the situation ((cdr (cons 3 5))) which will attempt to apply 5.

In short: (cc #f) will return a value to the context in which (call/cc _) appears. Here returning #f to that context implies that the or-expression will return the pair (cons 3 5) and thus the ((cdr X)) will fail.