s c h e m a t i c s : c o o k b o o k

/ Cookbook.IdiomPrematureReturn

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Returning prematurely from a function

Problem

You are either

Solution

The solution is to use an escape continuation. The form (let/ec escaper body ...) binds to escaper to a procedure of one argument, that when called will return the argument as the result of the entire let/ec form. 500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

A more Schemely example is multiplication of numbers. This function muliplies the numbers in a list. The function accumulates the result in the variable product. 500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

Now one observes that if zero occurs in the list the result will also be zero, no matter what the remaining elements in the list are. The form let/ec lets us add this case without altering the structure of the code.

500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

The difference between the two is illustrated by: 500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

Discussion

In plain R5RS you can use this definition: 500 Can't connect to 127.0.0.1:8778 (connect: Connection refused)

If your implementation provide escape continuations or one-shot continuations, then using these will probably be more efficient.

Contributors

-- JensAxelSoegaard

Comments

Probably some note should be made that C functions with premature return statements can be rewritten in Scheme using recursion and conditionals rather than explicit escape continuations. I'm not sure what is a good way to do this, however, since any programmer skilled in a language without tail-call optimization has a healthy aversion to recursion when not absolutely necessary. For PLT users for whom the Cookbook is an aid to migrating from another language, perhaps there should be topic like IdiomRecursiveProgramming?, and this IdiomPrematureReturn topic should plug recursive programming as something "advanced" Scheme programmers generally do. Although even programmers experienced in recursive programming sometimes find good use for let/ec. (Perhaps I'm misrepresenting "advanced Scheme programmers" here.)

-- NeilVanDyke - 19 May 2004

I have changed the discussion a little to emphasize that let/ec can be used in cases where you want to return prematurely from control struct even when haven't got access to it's code. (Which is why I chose a do loop and not a named let ;-) )

-- JensAxelSoegaard - 22 May 2004

CookbookForm
TopicType: Recipe
ParentTopic: CommonIdiomsRecipes?
TopicOrder:

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest