caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jon Harrop <jon@ffconsultancy.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] More problems with memoization
Date: Sun, 1 Oct 2006 01:23:48 +0100	[thread overview]
Message-ID: <200610010123.48846.jon@ffconsultancy.com> (raw)
In-Reply-To: <20060930200125.bjpzgnh7kkkogkgk@webmail.etu.upmc.fr>

On Saturday 30 September 2006 19:01, Diego Olivier FERNANDEZ PONS wrote:
> I wrote the following (classical) memoized code for the fibonacci
> function and I have been unsuccessfully trying to generalize it with a
> higher order function.
>
> let rec fib = function
>
>    | 0 -> 0
>    | 1 -> 1
>    | n -> fib_mem (n - 1) + fib_mem (n - 2)
>
> and fib_mem =
>    let table = ref [] in
>      function n ->
>        try
> 	List.assoc n !table
>        with Not_found ->
> 	let f_n = fib n in
> 	  table := (n, f_n) :: !table;
> 	  f_n
>
> # val fib : int -> int = <fun>
> # val fib_mem : int -> int = <fun>
>
> It works: fib 35 answers instantaneously.
>
> Now I want to achieve the same result with a higher order function
> [make_memo] and apply it to fib

I believe you want to "untie the knot" of recursion, creating an higher-order, 
auxiliary fibonacci function fib_aux that accepts the recursive call as an 
argument:

# let rec fib_aux fib = function
    | 0 | 1 as n -> n
    | n -> fib(n - 1) + fib(n - 2);;
val fib_aux : (int -> int) -> int -> int = <fun>

You can recover the ordinary fibonacci function using the Y combinator:

# let rec fib n = fib_aux fib n;;
val fib : int -> int = <fun>

You can write a higher-order memoization function that accepts an argument 
with the type of fib_aux:

# let memoize f =
    let m = Hashtbl.create 0 in
    let rec f' n =
      try Hashtbl.find m n with Not_found ->
        let x = f f' n in
        Hashtbl.replace m n x;
        x in
    f';;
val memoize : (('a -> 'b) -> 'a -> 'b) -> 'a -> 'b = <fun>

Now you can memoize recursive functions easily:

# memoize fib_aux 35;;
- : int = 9227465

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists


  parent reply	other threads:[~2006-10-01  0:23 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-09-30 18:01 Diego Olivier FERNANDEZ PONS
2006-09-30 19:19 ` [Caml-list] " Tom
2006-09-30 19:26   ` Tom
2006-10-01  0:23 ` Jon Harrop [this message]
2006-10-01  0:51   ` Martin Jambon
2006-10-02 15:29   ` Diego Olivier FERNANDEZ PONS
2006-10-02 23:00     ` Andrej Bauer
2006-10-02 23:04       ` Andrej Bauer
2006-10-03  0:50       ` skaller
2006-10-02 23:37     ` Don Syme

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=200610010123.48846.jon@ffconsultancy.com \
    --to=jon@ffconsultancy.com \
    --cc=caml-list@yquem.inria.fr \
    /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).