caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Yaron M. Minsky" <yminsky@CS.Cornell.EDU>
To: SooHyoung Oh <shoh@duonix.com>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] [q] on implementing of "memoization"
Date: 12 Apr 2003 10:34:01 -0400	[thread overview]
Message-ID: <1050158041.12890.48.camel@dragonfly.localdomain> (raw)
In-Reply-To: <000e01c30091$b66bc580$fe00a8c0@hama>

One thing you might want to try to keep track of is exceptions.  That
way, if a given memoized function throws an exception the first time, it
will keep on throwing that exception.

Another aproach I've found useful is cached memoization. Ttahat is, you
maintain an LRU cache of bounded size for the memoization.    You can
have the memoization function return both a ref to the maximum cache
size (so you can change it later) and the actual memoized function.

y

On Fri, 2003-04-11 at 21:20, SooHyoung Oh wrote:
> In testing "memoization" of fib function, it is OK on
>     let rec fib = ... and memo_fib n = memoize fib n;;
> but it makes the ERROR on
>     let rec fib = ... and memo_fib = memoize fib;;
> 
> The error message is
>       This kind of expression is not allowed as right-hand side of `let rec'
> 
> Why does this error occur?
> I can't find good description on ocaml manual.
> 
> One more thing:
> Do you have any idea about ** generic ** and good implementation of
> memoization?
> 
> The source is
> ----------------------------------
> let lookup key table = List.assoc key !table;;
> let insert key value table = table := (key, value) :: !table;;
> 
> let table = ref [];;
> let memoize f n =
>     try
>         lookup n table
>     with
>     | Not_found ->
>         let result = f n in
>         insert n result table;
>         result
> ;;
> 
> let rec fib = function
>     | 0 -> 0
>     | 1 -> 1
>     | n -> memo_fib (n-1) + memo_fib (n-2)
> and memo_fib n = memoize fib n
> ;;
> ----------------------
> 
> Thanks a lot!
> 
> ---
> SooHyoung Oh
> 
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
-- 
|--------/            Yaron M. Minsky              \--------|
|--------\ http://www.cs.cornell.edu/home/yminsky/ /--------|

Open PGP --- KeyID B1FFD916 (new key as of Dec 4th)
Fingerprint: 5BF6 83E1 0CE3 1043 95D8 F8D5 9F12 B3A9 B1FF D916



-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2003-04-12 14:34 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-04-12  1:20 SooHyoung Oh
2003-04-12 14:34 ` Yaron M. Minsky [this message]
2003-04-12 15:54 ` Neel Krishnaswami

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1050158041.12890.48.camel@dragonfly.localdomain \
    --to=yminsky@cs.cornell.edu \
    --cc=caml-list@inria.fr \
    --cc=shoh@duonix.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).