Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] fib with lazy list
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Michel Quercia <michel.quercia@p...>
Subject: Re: [Caml-list] fib with lazy list
Le Wed, 23 Apr 2003 19:53:28 +0900 "SooHyoung Oh" <shoh@duonix.com>
écrivit:

> I'm trying to implement "fib" function with lazy list.
> I met the error with message:
>     File "eg1.ml", line 19, characters 19-35:
>     This kind of expression is not allowed as right-hand side of `let
>     rec'
> 
> Can anyone solve this problem?
> 
> let rec fib_list = map fib (from 0)    (* ERROR *)
> and fib n =
>     match n with
>     | 1 -> 1
>     | n -> nth fib_list (n-1) + nth fib_list (n-2)

let rec x = ... x ... is allowed only when the right hand side is a functionnal value or "x is inside an argument of some constructor in the rhs". The following code should work (the constructor here is LzCons) :

let rec fib_list = LzCons(1, lazy(map fib (from 1)))
and fib n =
    match n with
    | n when n < 0 -> failwith "invalid argument"
    | 0 -> 1
    | 1 -> 1
    | _ -> nth fib_list (n-1) + nth fib_list (n-2)
;;

-- 
Michel Quercia
23 rue de Montchapet, 21000 Dijon
http://michel.quercia.free.fr (maths)
http://pauillac.inria.fr/~quercia (informatique)
mailto:michel.quercia@prepas.org

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners