Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Functional > Re: a question ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 16 of 18 Topic 2714 of 3037
Post > Topic >>

Re: a question about memoization

by Jon Harrop <usenet@[EMAIL PROTECTED] > Jan 4, 2008 at 10:40 PM

David B. Benson wrote:
> Here is a sketch of memoizing in SML, using the
> polymorphic sup****t for hash tables, HashTable,
> in the SML/NJ utility library:
> 
> signature MEMOIZE =
> sig
>   type hash_table_args =
>     {hash_fn: 'a -> word, eq_pred: 'a * 'a -> bool}
>   val memoize : hash_table_args
>                   -> ('a -> 'b) -> ('a -> 'b)
>   val memoized : ('a,'b)HashTable.hash_table
>                   -> ('a -> 'b) -> 'a -> 'b
> end
> 
> structure Memoize : MEMOIZE =
> struct
>   type hash_table_args =
>     {hash_fn: 'a -> word, eq_pred: 'a * 'a -> bool}
>   fun memoized ht f a =
>         (case HashTable.find ht a
>            of SOME b => b
>             | NONE =>
>                 let
>                   val b = f a
>                 in
>                   HashTable.insert ht (a,b); b
>                 end
>         (*esac*))
>   fun memoize {hash_fn,eq_pred} f a
>         let
>           val ht = HashTable.mkTable (hash_fn,eq_pred)
>                      (32,LibBase.NotFound)
>         in
>           memoized ht f a
>         end
> end (*structure Memoize*);
> 
> so it is clear that for each type 'a, the programmer
> has to supply a hash function and an equality predicate.
> While type 'a need not be a 'structural type', oft it is.
> What is the corresponding code in O'Caml and F#?

In F#:

  let memoize f =
    let m = Hashtbl.create 1
    fun x ->
      try m.[x] with Not_found ->
      let f_x = f x
      m.[x] <- f_x
      f_x

The critical point is that the hash and equality functions are carried in
the type automatically and do not need to be supplied separately by hand.
This solution combined the correctness of SML with brevity of OCaml.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/?u
 




 18 Posts in Topic:
a question about memoization
Michele Simionato <mic  2008-01-02 08:07:26 
Re: a question about memoization
Dr Jon D Harrop <jon@[  2008-01-02 08:52:39 
Re: a question about memoization
stephen@[EMAIL PROTECTED]  2008-01-05 06:03:47 
Re: a question about memoization
Jon Harrop <usenet@[EM  2008-01-05 09:26:13 
Re: a question about memoization
stephen@[EMAIL PROTECTED]  2008-01-05 17:36:40 
Re: a question about memoization
Jon Harrop <usenet@[EM  2008-01-05 19:39:46 
Re: a question about memoization
Vesa Karvonen <vesa.ka  2008-01-02 17:27:34 
Re: a question about memoization
Michele Simionato <mic  2008-01-02 23:53:04 
Re: a question about memoization
Vesa Karvonen <vesa.ka  2008-01-03 13:32:37 
Re: a question about memoization
Paul Rubin <http://phr  2008-01-03 05:37:47 
Re: a question about memoization
Michele Simionato <mic  2008-01-03 06:23:13 
Re: a question about memoization
Jon Harrop <usenet@[EM  2008-01-03 17:16:05 
Re: a question about memoization
Michele Simionato <mic  2008-01-03 09:40:41 
Re: a question about memoization
Jon Harrop <usenet@[EM  2008-01-04 07:26:32 
Re: a question about memoization
"David B. Benson&quo  2008-01-04 13:30:26 
Re: a question about memoization
Jon Harrop <usenet@[EM  2008-01-04 22:40:15 
Re: a question about memoization
"David B. Benson&quo  2008-01-04 15:38:03 
Re: a question about memoization
Jon Harrop <usenet@[EM  2008-01-04 23:45:56 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Oct 10 13:29:52 CDT 2008.