After some time of doing only toy tests and book exercises, I'm getting deeper into Scheme for a real application. One of the things I missed was Stackless Python's simple to use coroutine API. In Scheme you have
- raw continuations, which I find a bit too wild and low-level for my current purposes, or
- full-fledged preemptive threading systems, which seemed way too complex for my needs. Besides, these are implementation specific, and I didn't want to marry any implementation yet.
So I decided to give it a try and roll my own on plain R5RS (I'm sorry it's quite verbose as a result. I may reduce noise with macros in the future, but this is my first shot at it). It was easier than I was afraid, and great fun!
If you are into Scheme/Lisp, please tell me what you think about it. [Edit: I'm especially interested in comments on the API and semantics. This is more frugal than Stackless's, but I don't miss anything worthy. Do you?]
http://pastebin.com/ff732013
NOTE: This is just a one day hack; use it at your own risk. If you are interested in getting future versions of this, just write me at the address in the profile and I'll ping you when I'm confident it's reliable.
http://pastebin.com/d631795cd
My original code was not really R5RS compliant: continuation objects must be called with exactly one argument, unless the receiving end is explicitly expecting otherwise. Gambit C didn't complain, but Chicken did. Now this runs on both.