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'"
But as the function parameter of function f is used only for a recursive
call I believe that the function I try the define is at least "morally"
Is there a way to express this sort of construction in OCaml ? My aim is
to be able to have some things equivalent to:
let rec g = f g
let rec h = t (f h)
where t is some transformation over the function (conserving its type),
and still writing the code for f only once.
Caml-list mailing list. Subscription management:
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
let rec g x = f g x
let l = a::b::
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).
On Fri, Oct 23, 2009 at 12:34 AM, St�phane Glondu <st...@glondu.net> wrote:
> Mathias Kende a �crit :
>> � � � let rec g = f g
> What about:
> �let rec g x = f g x
En r�ponse au message
de : Lukasz Stafiniak
du : 2009-10-23 01:10:37
� : caml-list
Sujet : Re: [Caml-list] forbidden construct as right hand side of "let rec"
I don't know the exact rule, but I guess that on the right-hand side
let rec defining a ground value named foo you can only write a term
evaluates to a finite ground term on the currently defined variables +
That is to say something that evaluates to a finite tree of
constants or defined variables as leaves.
Maybe someone more knowledgeable could state the exact rule.
P.S. : the code using Obj is far from a solution as it modifies the
of the list to add cycling and thus, breaks persistency.
Le 23 oct. 2009 � 17:35, Damien Guichard a �crit :
You can find this in the documentation :
> P.S. : the code using Obj is far from a solution as it modifies the existing
> of the list to add cycling and thus, breaks persistency.
Well, you can easily copy the list before using Obj, wich preserves persistency.
Here is a relevant discussion on the list :
You can also look at Matias Giovannini's articles on his blogs (wich
are recommended reading anyway) :
This will compile, but then I also want to write :
let rec h = t (f h)
(with t : ('a -> 'b) -> 'a -> 'b) but here, I can't afford to use
let rec h x = t (f h) x
because t as some side effects and I need it to be evaluated only once.
Any idea on how to do that ?
Then what about:
let h =
let tmp = ref (fun x -> assert false) in
let res x = !tmp x in
(fun x ->
let y = t (f res) in
tmp := y;
Intuitively, the "tmp" reference caches the call to "t (f h)", but this
is otherwise the same technique as I gave earlier.
> 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
# 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::
> 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
Thank you, it makes sense!