简体   繁体   中英

understanding call/cc in scheme

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM