caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Andrej Bauer <Andrej.Bauer@fmf.uni-lj.si>
To: Diego Olivier FERNANDEZ PONS <diego.fernandez_pons@etu.upmc.fr>,
	caml-list@inria.fr
Subject: Re: [Caml-list] More problems with memoization
Date: Tue, 03 Oct 2006 01:00:22 +0200	[thread overview]
Message-ID: <45219A06.6080909@fmf.uni-lj.si> (raw)
In-Reply-To: <20061002172902.93z4c75nj4c8c884@webmail.etu.upmc.fr>

Diego Olivier FERNANDEZ PONS wrote:
> In my first example you keep the type of [fib] and add a second function 
> [fib_mem]. You can use anyone indifferently and hide the latter with the 
> .mli
> val fib : int -> int = <fun>
> val fib_mem : int -> int = <fun>

If you want to keep the same type for fib, and have the memoized one, as 
well as to have locality you can do something like this:

let make_memo f = ...

let rec make_rec f x = f (make_rec f) x

let fib, fib_mem =
   let fib' self = function
     | 0 -> 0
     | 1 -> 1
     | n -> self (n - 1) + self (n - 2)
   in
     make_rec fib', make_mem fib

(You will notice that make_rec is just the Y combinator.)

> When you compare your solution with what I am trying to do you see there 
> is a big difference in locality and transparency

I fail to see this big difference, frankly, since all you're doing is 
just a beta-reduction of what Jon and I suggested.

A recursive function _is_ the fixed point of a non-recursive one with an 
"extra" argument. You may hide this fact if you wish, but I think it's 
more honest to admit it to yourself. The "untied" version of fib has the 
advantage that you can do many cool things to it: memoizing is just one 
possibility.

Andrej


  reply	other threads:[~2006-10-02 23:00 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
2006-10-01  0:51   ` Martin Jambon
2006-10-02 15:29   ` Diego Olivier FERNANDEZ PONS
2006-10-02 23:00     ` Andrej Bauer [this message]
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=45219A06.6080909@fmf.uni-lj.si \
    --to=andrej.bauer@fmf.uni-lj.si \
    --cc=Andrej.Bauer@andrej.com \
    --cc=caml-list@inria.fr \
    --cc=diego.fernandez_pons@etu.upmc.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).