caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jan Kybic <kybic@fel.cvut.cz>
To: Erik de Castro Lopo <mle+ocaml@mega-nerd.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Memoization
Date: 30 Sep 2006 10:49:55 +0200	[thread overview]
Message-ID: <m2fye9lowc.fsf@fel.cvut.cz> (raw)
In-Reply-To: <20060909103332.0397efea.mle+ocaml@mega-nerd.com>

> While searching Google for info about memoization I found this

Hope this helps: A very simple memoization (not written by me) is 

   (* [memo f] creates a memoized version of a single parameter function [f]  *)
   val memo : ('a -> 'b) -> ('a -> 'b) 


   let memo f =  
     let h = Hashtbl.create 11 in 
     fun x -> try  
       Hashtbl.find h x  
     with Not_found ->  
       let r = f x in ( Hashtbl.add h x r; r ) 

This can be extended to function of two parameters as follows:

   let memo2 f = curry ( memo ( uncurry f ) )                        

   let uncurry f (x,y) = f x y

   let curry f x y = f (x,y) 

I further implemented a memoization system for my application based on
hash tables which also allows selective forgetting of cached values if
the memory is short. Let me know if you need it.

Jan

-- 
-------------------------------------------------------------------------
Jan Kybic <kybic@fel.cvut.cz>                       tel. +420 2 2435 5721
http://cmp.felk.cvut.cz/~kybic                      ICQ 200569450


      parent reply	other threads:[~2006-09-30  8:51 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-09-09  0:33 Memoization Erik de Castro Lopo
2006-09-09  3:38 ` [Caml-list] Memoization Andrej Bauer
2006-09-09  7:56   ` Erik de Castro Lopo
2006-09-09 15:47     ` Mike Lin
2006-09-09 21:20 ` William Neumann
2006-10-06  3:22   ` Walid Taha
2006-09-30  8:49 ` Jan Kybic [this message]

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=m2fye9lowc.fsf@fel.cvut.cz \
    --to=kybic@fel.cvut.cz \
    --cc=caml-list@inria.fr \
    --cc=mle+ocaml@mega-nerd.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).