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

Re: "ocaml_beginners"::[] A function that builds a circular list ?

Expand Messages
  • fabricemarchant
    ... Hi ! Remi Vanicat and Virgile Prevosto solved the problem in using the Lazy module : type a lazylist = Cons of a * a lazylist lazy_t | Nil let circular
    Message 1 of 29 , Jun 30, 2007
    • 0 Attachment
      > There is also the hideous solution consisting in using Obj, consider
      > the following for exemple :
      >
      > let circle (l:'a list) =
      > let rec last l =
      > if Obj.tag (Obj.field l 1) <> 0 then
      > l
      > else
      > last (Obj.field l 1) in
      > Obj.set_field (last (Obj.repr l)) 1 (Obj.repr l);;
      > let l = [1;2;3;4;5];;
      > circle l;;
      > l;;
      >
      > But I don't think this is good (aka Caml) programming to use such thing.

      Hi !

      Remi Vanicat and Virgile Prevosto solved the problem in using the Lazy
      module :

      type 'a lazylist = Cons of 'a * 'a lazylist lazy_t | Nil

      let circular n =
      let rec helper n l =
      if n = 0 then
      Lazy.force l
      else
      Cons (n, lazy (helper (n-1) l)) in
      let rec l = lazy (helper n l) in
      l

      let print_circular cl =
      let rec aux l =
      match l with
      Nil -> Printf.printf "Empty list!%!"
      | Cons(content,next) ->
      if (Lazy.force next) == cl then
      Printf.printf "%d\n" content
      else begin
      Printf.printf "%d, " content;
      aux (Lazy.force next)
      end
      in aux cl;;

      Looking at Lazy module source, it appears that Obj is heavily used
      like in Mathias Kende code.

      According to all we could read on this list, we are warned by Damien
      Doligez that :

      " some purple magic is going on here. Do not take this file
      as an example of how to program in Objective Caml."

      The Lazy module helps to work with frozen evaluation.
      I wonder if the ability to build circular structures (using lazyness
      and not referncs) mandatorily relies on Obj ?

      Would it be possible to rewrite the above functions "circular" and
      "print_circular" with "hand made" basic lazyness i.e. in freezing
      things using unit functions ?

      I tried to mimic the above code, translating "lazy" thing to (fun ()
      -> thing ) and "Lazy.force" being translated to the () function call :
      http://fabrice.marchant.free.fr/code/circunit.ml.html

      But I could only print compile time builded structures.
      print_circular ( build_circular2 c3 ) never stops...

      If you have any light about this ...

      Fabrice
    • Fabrice Marchant
      Hi ! ( Replying to myself. ) ... The answer is yes : http://fabrice.marchant.free.fr/code/circul.ml.html So we can generate circular structures without any
      Message 2 of 29 , Jul 1, 2007
      • 0 Attachment
        Hi !

        ( Replying to myself. )
        I wrote :
        > Would it be possible to rewrite the above functions "circular" and
        > "print_circular" with "hand made" basic lazyness i.e. in freezing
        > things using unit functions ?

        The answer is yes :

        http://fabrice.marchant.free.fr/code/circul.ml.html

        So we can generate circular structures without any "ref" and without calling Obj ( even indirectly by Lazy ).

        Fabrice
      Your message has been successfully submitted and would be delivered to recipients shortly.