Steele and Sussman | March 10, 1976 | 13 | LAMBDA: The Ultimate Imperative |

### Escape Expressions

Reynolds [Reynolds 72] defines the construction

escape

xinr

to evaluate the expression

in an environment such that the variable *r*

is bound to an *x*__escape function__. If the escape function is never applied, then the value of the escape expression is the value of

. If the escape function is applied to an argument *r**a*, however, then evaluation of

is aborted and the escape expression returns *r**a*. {Note J-operator} (Reynolds points out that this definition is not quite accurate, since the escape function may be called even after the escape expression has returned a value; if this happens it "returns again"!)

As as example of the use of an escape expression, consider this procedure to compute the harmonic mean of an array of numbers. If any of the numbers is zero, we want the answer to be zero. We have a function

which will sum the reciprocals of numbers in an array, or call an escape function with zero if any of the numbers is zero. (The implementation shown here is awkward because ALGOL requires that a function return its value by assignment.)
*harmsum*

begin

real procedureharmsum(a,n,escfun);

real arraya; integern; real procedureescfun(real);

begin

realsum;

sum:= 0;

fori:= 0 untiln-1 do

begin

ifa[i]=0 thenescfun(0);

sum:=sum+ 1/a[i];

end;

real arrayb[0:99];

print(escapexin 100/harmsum(b, 100,x));

end

If

exits normally, the number of elements is divided by the sum and printed. Otherwise, zero is returned from the escape expression and printed without the division ever occurring.
*harmsum*

This program can be written in SCHEME using the built-in escape operator `CATCH`

: