4

Maybe my question has a really simple answer, but I cannot find it.

In Scheme R6RS how can I built a continuation that does nothing and requires any arguments?

My goal is to have a continuation, let's name it QUIT such that if I have the following code:

((lambda ()
  (display 1)
  (display 2)
  (QUIT)
  (displey "A")))

it preints 1 and 2 but not "A".+

Can you help me?

Thank you.

Aslan986
  • 9,984
  • 11
  • 44
  • 75

1 Answers1

5

The most straightforward way is to use a simple "return" style continuation:

(call/cc (lambda (return) 
    (display 1) 
    ...
    (return) 
    (display "A")))

Does that help at all?

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • Yep, thank you. I was interested in a definition of a return function that I can reuse; so, taking inspiration from your answer I did: `(define quit #f)` and then `(call/cc (lambda (k) (set! quit k)))`. – Aslan986 Apr 30 '12 at 15:32
  • @Justin What happens there is that call/cc backs the context up, and calling (return) within the lambda makes the program continue jump to the instruction following the call to call/cc, am I right ? – dader Nov 30 '12 at 11:32
  • 1
    @dader - Yes, this is one of the simplest and most straightforward uses of a continuation. In fact, it can be referred to as an "escape continuation" since it restores control to the enclosing scope. – Justin Ethier Nov 30 '12 at 14:43
  • May I assume that continuation would be of better use for example into a backtracking recursive descent parser, instead of Exception for triggering backtrack ? – dader Dec 03 '12 at 10:24
  • 1
    @dader - Continuations are a generic way to implement control flow, much like a GOTO, and as such are of good use in both cases. Consider, for example, that continuations are the only way (or at least, the simplest way) to implement `return` in Scheme. – Justin Ethier Dec 03 '12 at 14:51