Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!munnari.oz.au!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.scheme Subject: Re: catch/throw in scheme - how to ?? Message-ID: <6246@goanna.cs.rmit.oz.au> Date: 13 Jun 91 05:00:56 GMT References: <1991Jun10.124127.17926@eua.ericsson.se> Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 40 In article <1991Jun10.124127.17926@eua.ericsson.se> joe@erix.ericsson.se (Joe Armstrong) writes: > Could sombody who understands these things kindly explain how > to do the equivalent of 'catch' and 'throw' in scheme?? - > can one use call/cc for such operations? - if so how? In article , jinx@zurich.ai.mit.edu (Guillermo J. Rozas) explained how to implement catch and throw on top of call/cc. I suggest that it is simpler to use call-with-current-continuation directly. For example, on p80 of Dybvig's book, we find "Here is a more practical example, showing the use of call/cc to provide a nonlocal exit from a loop. (define member (lambda (x ls) (call/cc (lambda (break) (do ((ls ls (cdr ls))) ((null? ls) #f) (if (equal? x (car ls)) (break ls))))))) " (I have corrected two gratuitous incompatibilities with the standard.) In general, the picture is (call-with-current-continuation (lambda (escape-function) ;; any amount of your code goes here. ;; When you want to return from the entire construct ;; with value x, just call (escape-function x) ;; and that can go anywhere you like. )) The advantage of catch and throw is that a procedure can "throw" to a "catch" which doesn't *lexically* enclose it. -- Q: What should I know about quicksort? A: That it is *slow*. Q: When should I use it? A: When you have only 256 words of main storage.