caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: mathias@kende.fr
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] forbidden construct as right hand side of "let rec"
Date: Wed, 28 Oct 2009 17:52:18 +0100	[thread overview]
Message-ID: <4AE876C2.8050707@inria.fr> (raw)
In-Reply-To: <1256250121.4178.37.camel@MATHIAS-ENS>

Mathias Kende wrote:

> I need to write something like this :
> 
> 	let f f i = if i = 0 then 1 else i * f (i - 1)
> 	let rec g = f g
> 
> Of course the compiler won't let me write it (even if the OCaml type
> system is happy):
> 	"This kind of expression is not allowed as right-hand side of `let rec'"
In general, the best thing to do in this case is to switch to lazy
evaluation:

# let f f i = if i = 0 then 1 else i * Lazy.force f (i-1);;
val f : (int -> int) Lazy.t -> int -> int = <fun>
# let rec g' = lazy (f g');;
val g' : (int -> int) Lazy.t = <lazy>
# let g = Lazy.force g';;
val g : int -> int = <fun>
# g 10;;
- : int = 3628800

Lukasz Stafiniak wrote:

> While we are at it, what is the best way to convert a "straight" list
> into a cyclic list?
> 
> i.e. convert
> 
> let l = a::b::[]
> 
> into
> 
> let rec l = a::b::l
> 
> (for arbitrary length lists). (The answer I recall from the archives
> was using Obj.magic to mutate the [] in the original list).

Obj.magic is not part of the OCaml language :-)

Again, you can do that just fine using lazy lists instead of lists:

type 'a lazylist = 'a lazylist_content Lazy.t
and 'a lazylist_content = Nil | Cons of 'a * 'a lazylist

Hope this helps,

- Xavier Leroy


  parent reply	other threads:[~2009-10-28 16:52 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-22 22:22 Mathias Kende
2009-10-22 22:34 ` [Caml-list] " Stéphane Glondu
2009-10-22 23:10   ` Lukasz Stafiniak
2009-10-23 15:35     ` Damien Guichard
2009-10-23 16:14       ` Marc de Falco
2009-10-23 17:51         ` blue storm
2009-10-25 14:11   ` Mathias Kende
2009-10-25 15:03     ` Stéphane Glondu
2009-10-28 16:52 ` Xavier Leroy [this message]
2009-10-28 22:44   ` Lukasz Stafiniak

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=4AE876C2.8050707@inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@yquem.inria.fr \
    --cc=mathias@kende.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).