Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!zaphod.mps.ohio-state.edu!wuarchive!uunet!europa.asd.contel.com!gatech!bloom-beacon!dont-send-mail-to-path-lines From: gyro@cymbal.reasoning.COM (Scott Layson Burson) Newsgroups: comp.lang.scheme Subject: C->Scheme mappings Message-ID: <9105302308.AA18221@cymbal.reasoning.com.> Date: 30 May 91 23:08:38 GMT References: <1991May28.191834.15025@apd.mentorg.com> Sender: daemon@athena.mit.edu (Mr Background) Reply-To: Gyro@reasoning.com Organization: The Internet Lines: 54 Date: 28 May 91 19:18:34 GMT From: "Joe Mueller @ APD x1367" I'm trying to map the various C control statements into Scheme. The translation of break, continue, and return are giving me trouble. (I am not a scheme programmer so an obvious answer may not be obvious to me) I suspect I need to do something with call-with-current-continuation but I don't see exactly how to do it. Anyone care to help? You don't need call-with-current-continuation. `return' is implicit in Scheme: a lambda body always returns the value of its last form. So you don't have to do anything special with it; e.g. int foo() { return 3; } --> (define (foo) 3) The way `break' and `continue' work is tied up in the way iteration is modelled using tail-recursion. For instance for (i = 0; i < 10; ++i) { if (p1(i)) continue; /* ... <1> */ if (p2(i)) break; /* ... <2> */ } --> (letrec (((loop i) (if (not (< i 10)) #f (if (p1 i) (loop (+ 1 i))) ; `continue' ;; ... <1> (if (p2 i) #f ; `break' value necessary (begin ;; ... <2> (loop (+ 1 i))))))) (loop 0)) (Apologies if I screwed up the syntax -- I haven't written much Scheme lately.) Here you see that the `continue' has turned into `(loop (+ 1 i))', which invokes the next cycle of the loop, and `break' has turned into `#f' (it doesn't matter what the value is, but there has to be one). That is, `break' amounts to simply not invoking the next iteration. A bit mind-bending, eh? -- Scott