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

9995Re: "ocaml_beginners"::[] Function executed more than once in recursion?

Expand Messages
  • Martin Jambon
    Aug 1, 2008
    • 0 Attachment
      On Fri, 1 Aug 2008, hmf@... wrote:

      > Hello,
      >
      > I have what seems a strange result. Maybe I am overlooking
      > something obvious. I am using the CuDD library via Ocaml in
      > the following function:
      >
      > let rec create_vars = function
      > | [] -> []
      > | h::t -> begin
      > match h with
      > | Var(i,_) -> Printf.printf "New var = %d\n" i ;
      > (*(Bdd.newvar_at_level man i) :: create_vars t*)
      > let v = Bdd.newvar_at_level man i
      > in v :: create_vars t
      > | _ -> raise_source_pos_exception "Expected a variable." pos
      > end
      >
      > The function "Bdd.newvar_at_level man i" holds "state"
      > in that it increments the number of variables in the BDD.
      > When I use:
      > (Bdd.newvar_at_level man i) :: create_vars t
      > or
      > Bdd.newvar_at_level man i :: create_vars t
      >
      > Two additional variables in a list of three are created.
      > If I use:
      > let v = Bdd.newvar_at_level man i
      > in v :: create_vars t
      > then the number of variables is correct.
      >
      > I therefore conclude that in the first 2 version
      > the function is executed a second time, supposedly
      > when recursion is terminating.
      >
      > What am I doing wrong here?

      Without looking deeply at your code, I suspect that your problem is in:

      expr1 :: expr2

      You can't assume that expr1 will be evaluated before expr2.
      Always do:

      let x = expr1 in
      x :: expr2

      And sorry if it's not the cause of your problem...


      Martin

      --
      http://mjambon.com/
    • Show all 3 messages in this topic