caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "QUERCIA Michel (T) Math" <querciam@l-carnot1.ac-dijon.fr>
To: "'caml-list@pauillac.inria.fr'" <caml-list@pauillac.inria.fr>
Subject: recursive definition
Date: Mon, 22 Apr 96 12:16:00 PDT	[thread overview]
Message-ID: <317C0954@gw-msmail.ac-dijon.fr> (raw)

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2425 bytes --]



==========================================================================  

En francais :

Il a ete explique dans un post precedent que "let rec x = f(x)"
n'est compilable que si "x" est une fonction ou si la compilation
de "f(x)" ne depend pas de "x".

Qu'est-ce qui ne va pas dans la definition de "fib" ci dessous ?

==========================================================================  

In english :

I remember a post where it was explained that "let rec x = f(x)" is   
handled
only when "x" is a function or when "f(x)" may be compiled with no   
knowledge
of "x".

So, what is wrong with the following attempt to define "fib" ?

==========================================================================  


>       Caml Light version 0.7

#                        (* +----------------------+
                            |  Fonction a memoire  |
                            +----------------------+ *)


let remember(f) =
  let t = hashtbl__new(17) in
  fun x -> try hashtbl__find t x
           with Not_found -> let y = f(x) in hashtbl__add t x y; y
;;
remember : ('a -> 'b) -> 'a -> 'b = <fun>
#


let premier = remember(fun x ->
  printf__fprintf stderr "appel %d\n" x; flush stderr;
  let y = ref(2) in
  while !y * !y < x & x mod !y > 0 do incr y done;
  !y * !y > x
);;
premier : int -> bool = <fun>
#
map premier [1;2;3;4;5;6;7;8;9;10];;
appel 10
appel 9
appel 8
appel 7
appel 6
appel 5
appel 4
appel 3
appel 2
appel 1
 - : bool list =
 [true; true; true; false; true; false; true; false; false; false]
#
map premier [1;2;3;4;5;6;7;8;9;10];;
 - : bool list =
 [true; true; true; false; true; false; true; false; false; false]
#


let rec fib = remember(fun x ->
  if x < 2 then 1 else fib(x-1) + fib(x-2)
);;
Entrée interactive:
>..............remember(fun x ->
>  if x < 2 then 1 else fib(x-1) + fib(x-2)
>).......
Ce genre d'expressions n'est pas autorisé comme membre droit d'un "let   
rec".
#quit();;

Process inferior-caml finished

==========================================================================  


PS : J'ai aussi essaye :

let rec fib(x) = let g = remember(fun x -> ...) in g(x);;

Ca marche (a la compilation) mais il n'y a pas memorisation
des resultats puisque g est reconstruite a chaque appel.

==========================================================================  


Michel Quercia
Lycee Carnot
Dijon

email = querciam@ac-dijon.fr
Cc = quercia@u-bourgogne.fr  





             reply	other threads:[~1996-04-23 18:45 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-04-22 19:16 QUERCIA Michel (T) Math [this message]
1996-04-23 22:12 Tarizzo Martial
1996-04-24  0:38 Doug Currie, Flavors Technology, Inc.
1996-04-24 19:17 Damien Doligez
1996-04-25 21:41 Tarizzo Martial
1996-04-29  8:54 ` Xavier Leroy

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=317C0954@gw-msmail.ac-dijon.fr \
    --to=querciam@l-carnot1.ac-dijon.fr \
    --cc=caml-list@pauillac.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).