Re: re: How to return from a function

From: Tom Hicks <>
Date: Mon, 20 Jan 1997 00:44:44 -0700

David Fox <> writes:

>Date: Sun, 19 Jan 1997 20:01:44 -0500
>From: David Fox <>
>Organization: NYU Media Research Lab.
>Subject: Re: How to return from a function

>"John L. Stein" <> writes:

>> How can I return from a function?
>> In c you'd use
>> if A return;
>> But there is no return in STk.

>There is no explicit return in Scheme. Instead of "if A return"
>write "(if (not A) (whatever came after the return))"
>David Fox xoF divaD
>NYU Media Research Lab baL hcraeseR aideM UYN

Mr. (Dr.?) Fox is correct in pointing out that proper ordering
of tests might solve your problem. Scheme also has, however, a
fascinating mechanism for remembering (and then continuing from)
a specific point in a computation. The "call/cc" function may be
used achieve the effect of a C "return" (although it is really
more like a C "catch/throw") as follows:

(define yourfunc (...)
  (call/cc (lambda (return)
      (if a ; if A is True
        (return avalu) ; exit 'yourfunc' returning 'avalu'
        (more-processing...) ; else do more processing

The quick and dirty explanation of what happens here is that the
call-with-current-continuation function (call/cc) saves
"the computation remaining from this point" (called a 'continuation')
Since the call/cc function is the outermost enclosing expression of
'yourfunc', the "computation remaining" is to return a value from
your function. The value returned is the value given to the continuation
within the IF. Note that I have named the variable to which the
continuation is bound, 'return' to indicate how the continuation
functions in this case. This could be potentially confusing to
programmers of other languages who are used to a more rigid distinction
between control structures and data.

        -Tom Hicks
Received on Mon Jan 20 1997 - 08:44:25 CET

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST