Loading ...
Sorry, an error occurred while loading the content.
 

Re: "ocaml_beginners"::[] Re: why does this recursion not work?

Expand Messages
  • Peng Zang
    ... Hash: SHA1 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
    Message 1 of 7 , Jun 1, 2007
      -----BEGIN PGP SIGNED MESSAGE-----
      Hash: SHA1

      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?

      Peng

      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)

      iD8DBQFGYK7tfIRcEFL/JewRArD1AJ9KXg3JuUNKI3HIEUF8u0Qcae5QLgCgvuYy
      ndGX5GanftdcCcBvTKv2k+4=
      =jH1x
      -----END PGP SIGNATURE-----
    Your message has been successfully submitted and would be delivered to recipients shortly.