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 15 of 18 Topic 2714 of 2986
Post > Topic >>

Re: a question about memoization

by "David B. Benson" <dbenson@[EMAIL PROTECTED] > Jan 4, 2008 at 01:30 PM

On Jan 2, 8:52 am, Dr Jon D Harrop ... wrote
....
> You want structural ha****ng, which is one of the developments added by
> OCaml and improved by F#.
....

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#?
 




 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 Sun Sep 7 2:32:16 CDT 2008.