简体   繁体   中英

Continuation in Scheme with lambda with no arguments

So I am dealing with continuations and have something like this:

(or
    (call/cc (lambda (cont)
           ...
          (if ( ... )
              (cons randomList (lambda() (cont #f)))
              #f)})}
     ( do something else)

I was wondering what the difference between (lambda() (cont #f)) and (cont #f) are. I get the answer I want with the lambda and something wrong without. Could someone explain the difference? Thanks.

A nullary (zero arguments) lambda used in this way is called a thunk .

Thunks are used in Scheme to defer the execution of some piece of code. Suppose, for example, that we're talking about (display #f) instead of (cont #f) . If you wrote (display #f) directly, then when the code execution reached that point, it'd display #f straight away, whereas if you wrapped it in a thunk ( (lambda () (display #f)) ), it wouldn't display the #f until you invoked the thunk.

Back to your code, a (cont #f) in the code would cause an immediate jump at the point where the continuation is invoked. Wrapping it in a thunk delays the invocation of the continuation until you invoke the thunk.

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