Version française
Home     About     Download     Resources     Contact us    
Browse thread
re: help an o'caml beginner
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Lenny Gray <lgray@s...>
Subject: re: help an o'caml beginner
At 11:51 7/26/2000 -0600, 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.

Of course a trivial approach would be to accept the fact
that O'Caml _has_ true assignable variables, via references.

However, since you sound like you want the _functional_
answer, the insight you're looking for is:

let rec  f v1 v2 i  =  if i=0 then v2   else f v2 (v1+v2) (i-1);;

then:  f 0 1 0  == 1
       f 0 1 1  == 1
       f 0 1 2  == 2
       ...
       f 0 1 10 == 89


- Lenny -