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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 651 - (view) (download)

1 : blume 494 (*
2 :     * Simple memoization.
3 :     *
4 :     * (C) 1999 Lucent Technologies, Bell Laboratories
5 :     *
6 :     * Author: Matthias Blume (blume@kurims.kyoto-u.ac.jp)
7 :     *)
8 :     structure Memoize :> sig
9 :     (* If g is the result of (memoize f), then calling g the first time
10 :     * will result in f being called with the same argument.
11 :     * Any subsequent call to g simply returns the result that was
12 :     * computed during the first call. Thus, f will be called at most
13 :     * once on g's behalf. *)
14 :     val memoize : ('a -> 'b) -> ('a -> 'b)
15 :     end = struct
16 :     fun memoize f = let
17 :     val cache = ref (fn _ => raise Fail "Memoize.memoize: uninitialized")
18 :     fun first_time x = let
19 :     val v = f x
20 :     fun later_on _ = v
21 :     in
22 :     cache := later_on;
23 :     v
24 :     end
25 :     in
26 :     cache := first_time;
27 :     (fn x => !cache x)
28 :     end
29 :     end

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