Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Alain Frisch <frisch@clipper.ens.fr>
To: John BEPPU <beppu@lineo.com>
Cc: caml-list@inria.fr
Subject: Re: help an o'caml beginner
Date: Thu, 27 Jul 2000 20:47:46 +0200 (MET DST)	[thread overview]
Message-ID: <Pine.GSO.4.04.10007272037290.1683-100000@clipper.ens.fr> (raw)
In-Reply-To: <20000726115152.A6102@yukari.lineo.com>

On Wed, 26 Jul 2000, John BEPPU wrote:

> >>>
> If anyone out there could implement an O'Caml fibonacci number
> generator that doesn't make redundant calculations, I would really
> appreciate it.  I just need something to study.
> >>>

(I guess there will be many answers ..)

Here is a solution:

let fibo n =
  let rec aux last x i =    (* x is u_i; last is u_{i-1} *)
     if (i = n) then x
     else aux x (x + last) (i + 1)
  in
  if (n = 0) then 1 else
  aux 1 1 1


> I wrote a recursive Perl version that doesn't make redundant
> calculations, and I'd be interested to see how different in
> structure an O'Caml version would be.
> 
>     sub fib_pair {
>         my $n = shift;
>         my @seq;
>         if ($n < 2) {
>             @seq = (1, 1);
>         } else {
>             @seq = fib_pair($n - 1);
>             @seq = ($seq[1], $seq[0] + $seq[1]);
>         }
>         return @seq;
>     }

Well, this function is not tail-recursive (the recursive call
can't be implemented as a jump). I didn't try it, but I am pretty sure
the OCaml version will be more efficient.

-- 
Alain Frisch



  reply	other threads:[~2000-07-27 21:29 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-07-26 17:51 John BEPPU
2000-07-27 18:47 ` Alain Frisch [this message]
2000-07-27 18:56 ` Markus Mottl
2000-07-29 17:03   ` John BEPPU
2000-07-29 17:23     ` Markus Mottl
2000-07-31  7:11   ` Vitaly Lugovsky
2000-07-31  8:43     ` Markus Mottl
2000-08-02 16:58     ` Printable Caml manual Walid Taha
2000-07-27 18:57 ` help an o'caml beginner Remi VANICAT
2000-07-27 19:08 ` Jean-Christophe Filliatre
2000-07-27 21:31 ` Laurent Chéno
2000-07-27 23:47 ` Michel Quercia
2000-07-27 18:24 Brent Fulgham
2000-07-27 18:43 Lenny Gray
2000-07-27 22:50 John R Harrison
2000-07-28 12:49 ` John BEPPU
2000-08-02 17:28 Brent Fulgham

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.GSO.4.04.10007272037290.1683-100000@clipper.ens.fr \
    --to=frisch@clipper.ens.fr \
    --cc=beppu@lineo.com \
    --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