caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Michel Quercia <michel.quercia@prepas.org>
To: "SooHyoung Oh" <shoh@duonix.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] fib with lazy list
Date: Wed, 23 Apr 2003 14:20:56 +0200	[thread overview]
Message-ID: <20030423142056.6e1ff593.michel.quercia@prepas.org> (raw)
In-Reply-To: <003901c30986$92677ff0$fe00a8c0@hama>

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


      parent reply	other threads:[~2003-04-23 12:21 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-04-23 10:53 SooHyoung Oh
2003-04-23 12:09 ` sebastien FURIC
2003-04-23 12:24   ` sebastien FURIC
2003-04-23 12:20 ` Michel Quercia [this message]

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=20030423142056.6e1ff593.michel.quercia@prepas.org \
    --to=michel.quercia@prepas.org \
    --cc=caml-list@inria.fr \
    --cc=shoh@duonix.com \
    /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).