Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] View of /sml/trunk/src/cm/util/memoize.sml
ViewVC logotype

View of /sml/trunk/src/cm/util/memoize.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 651 - (download) (annotate)
Thu Jun 1 18:34:03 2000 UTC (20 years ago) by monnier
File size: 785 byte(s)
bring revisions from the vendor branch to the trunk
(*
 * Simple memoization.
 *
 * (C) 1999 Lucent Technologies, Bell Laboratories
 *
 * Author: Matthias Blume (blume@kurims.kyoto-u.ac.jp)
 *)
structure Memoize :> sig
    (* If g is the result of (memoize f), then calling g the first time
     * will result in f being called with the same argument.
     * Any subsequent call to g simply returns the result that was
     * computed during the first call.  Thus, f will be called at most
     * once on g's behalf. *)
    val memoize : ('a -> 'b) -> ('a -> 'b)
end = struct
    fun memoize f = let
	val cache = ref (fn _ => raise Fail "Memoize.memoize: uninitialized")
	fun first_time x = let
	    val v = f x
	    fun later_on _ = v
	in
	    cache := later_on;
	    v
	end
    in
	cache := first_time;
	(fn x => !cache x)
    end
end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0