Looking for a contrived code example: continued!

So, I believe that now I understand what is happening, at least at some level thanks to the wiki community and Learn the scheme in the days of Fixnum .

But I would like to practice more - that is, more example code that I can do in my head (preferably invented so that there are no extraneous things to distract from the concept).

In particular , I would like to work with additional issues with continuation that resume and / or coroutines, and not just use them to exit the loop or something else (which is pretty simple).

In any case, if you know good tutorials other than the ones mentioned above, or if you want to publish something you wrote, that would be a good exercise, I would be very grateful!

+5
source share
2 answers

Yes, the sequel can be quite flexible. Here is a good puzzle that I found a while ago - try to figure out what is printed and why:

(define (mondo-bizarro)
  (let ((k (call/cc (lambda (c) c)))) ; A
    (write 1)
    (call/cc (lambda (c) (k c))) ; B 
    (write 2)
    (call/cc (lambda (c) (k c))) ; C
    (write 3)))

(mondo-bizarro)

Explanation of how it works (contains spoilers!):

  • The first vaults call/ccreturn its own sequel and store it in k.
  • The number is 1written to the screen.
  • The current continuation, which should continue at point B, returns to k, which returns to A
  • k , B
  • 1
  • , B, k, ( ) B
  • , , k A
  • 2
  • , C, k, A
  • k , C
  • 1
  • , B, k, C
  • 3

, 11213. , - , "reset", k k . , .

+5

, .

+4

All Articles