Re: "ocaml_beginners":: Re: why does this recursion not work?
- -----BEGIN PGP SIGNED MESSAGE-----
Ha! You're brilliant. That's exactly what I wanted. I didn't know you had
to explictly enable recursive types. Why isn't this enabled by default?
Slow compile times?? Safety?
On Thursday 31 May 2007 17:24, Zheng Li wrote:
> Peng Zang <peng.zang@...> writes:
> > -----BEGIN PGP SIGNED MESSAGE-----
> > Hash: SHA1
> > I forget what this kind of recursion is called and I cannot seem to get
> > it to work.
> > Here is an (failing) example for a function that counts how many times it
> > has been called with an even argument:
> > let numeven b =
> > let rec aux a b =
> > (a, aux (if b mod 2 = 0 then a + 1 else a))
> > in aux 0
> > It errors with: This expression has type int * (int -> int * 'a) but is
> > here used with type int * 'a
> > I know I can write a side-effecting version using a hidden ref, but I
> > need a functional version. Does any one know how do you do this in OCaml
> > and what is this type of thing is called?
> Enable recursive type, if that's what you really want.
> $ ocaml -rectypes
> Objective Caml version 3.09.2
> # let numeven b =
> let rec aux a b =
> (a, aux (if b mod 2 = 0 then a + 1 else a)) in
> aux 0;;
> val numeven : 'a -> (int -> int * 'b as 'b) = <fun>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.2 (GNU/Linux)
-----END PGP SIGNATURE-----