From: Thomas Fischbacher <Thomas.Fischbacher@Physik.Uni-Muenchen.DE>
To: caml-list@inria.fr
Subject: What on earth is this?
Date: Fri, 11 Nov 2005 21:18:08 +0100 (CET) [thread overview]
Message-ID: <Pine.LNX.4.61.0511112117490.6534@eiger.cip.physik.uni-muenchen.de> (raw)
Maybe I just need a good night of sleep, maybe I overlooked something
really dumb and rather should have asked on the beginner's list. Anyway,
I find the behaviour demonstrated below quite confusing. Why don't the
definitions ###_v1 and ###_v2 work? Any idea, anyone?
===>
(* (C) T. Fischbacher, 2005 - Demonstrating a simple form of memoizing... *)
#load "unix.cma";;
let timing ?(tagfun= fun _ -> "Time passed: ") ?(channel=stdout) f x =
let tag = tagfun x in
let t0 = Unix.gettimeofday () in
let result = f x in
let t1 = Unix.gettimeofday () in
let () = Printf.fprintf channel "%s%f sec\n%!" tag (t1-.t0) in
result
;;
let rec fibonacci n =
if n < 2 then 1 else fibonacci (n-1) + fibonacci (n-2)
;;
(*
# timing fibonacci 35;;
Time passed: 4.254157 sec
- : int = 14930352
*)
let memoized ht f arg =
try
Hashtbl.find ht arg
with
| Not_found ->
let result = f arg in
let () = Hashtbl.add ht arg result in
result
;;
let rec mem_fibonacci_v1 =
let mem = Hashtbl.create 10 in
memoized mem
(fun n -> if n < 2 then 1 else mem_fibonacci_v1 (n-1) +
mem_fibonacci_v1 (n-2))
;;
(* Error: "This kind of expression is not allowed as right-hand side of `let rec'" *)
let mem_fibonacci_v2 =
let mem = Hashtbl.create 10 in
let rec mf =
memoized mem
(fun n -> if n < 2 then 1 else (mf (n-1)) + (mf (n-2)))
in mf
;;
(* Error: "This kind of expression is not allowed as right-hand side of `let rec'" *)
let mem_fibonacci_v3 =
let mem = Hashtbl.create 10 in
let mf yfp =
memoized mem
(fun n -> if n < 2 then 1 else yfp () (n-1) + yfp () (n-2))
in
let rec yc x = x (fun () -> (yc x)) in
yc mf
;;
(* This definition worked. And indeed:
# timing mem_fibonacci_v3 35;;
Time passed: 0.000149 sec
- : int = 14930352
# timing mem_fibonacci_v3 35;;
Time passed: 0.000005 sec
- : int = 14930352
#
*)
<===
--
regards, tf@cip.physik.uni-muenchen.de (o_
Thomas Fischbacher - http://www.cip.physik.uni-muenchen.de/~tf //\
(lambda (n) ((lambda (p q r) (p p q r)) (lambda (g x y) V_/_
(if (= x 0) y (g g (- x 1) (* x y)))) n 1)) (Debian GNU)
next reply other threads:[~2005-11-11 20:18 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-11-11 20:18 Thomas Fischbacher [this message]
2005-11-11 21:22 ` [Caml-list] " Martin Jambon
2005-11-11 21:34 ` Thomas Fischbacher
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=Pine.LNX.4.61.0511112117490.6534@eiger.cip.physik.uni-muenchen.de \
--to=thomas.fischbacher@physik.uni-muenchen.de \
--cc=caml-list@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).