Local Methods

Local methods resemble bare methods but have names. They are declared within other methods, often as private utility routines. Local methods are typically used in a fashion similar to Pascal's local functions.

define method sum-squares(in :: <list>) => sum-of-element-squares :: <integer>;
  local method square( x )
          x * x;
        end,
        method sum(list :: <list>)
          reduce1(\+, list);
        end;
  sum(map(square, in));
end;
      

Local methods can actually outlive the invocation of the function which created them. parameters of the parent function remain bound in a local method, allowing some interesting techniques:

define method build-put(string :: <string>) => (res :: <function>);
  local method string-putter()
          puts(string);
        end;
  string-putter; // return local method
end;

define method print-hello() => ();
  let f = build-put("Hello!");
  f();				// print "Hello!"
end;
      

Local functions which contain bound variables in the above fashion are known as closures.