% lazy - Lazy evaluator, or memoiser
%
% lazy :: (unit -> A) -> (unit -> A).
% lazy :: (unit -> action A) -> (unit -> action A). 
%
% If x=lazy(f), then f is not called until x is
% called. Then x() returns x1=f(). Thereafter,
% x() returns x1 and f is never called again.
%
% Eg, let x=lazy(@()rand).
% Then x() returns the same random number each
% time it is called.
function f=lazy(thunk)
	value=thunk; 
	evalled=false;
	f=@get;

	function v=get, 
		if evalled, 
			v=value;
		else 
			value=thunk(); 
			evalled=true; 
			v=value; 
		end
	end
end
