Re: "ocaml_beginners":: A function that builds a circular list ?
> There is also the hideous solution consisting in using Obj, considerHi !
> the following for exemple :
> let circle (l:'a list) =
> let rec last l =
> if Obj.tag (Obj.field l 1) <> 0 then
> 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;;
> But I don't think this is good (aka Caml) programming to use such thing.
Remi Vanicat and Virgile Prevosto solved the problem in using the Lazy
type 'a lazylist = Cons of 'a * 'a lazylist lazy_t | Nil
let circular n =
let rec helper n l =
if n = 0 then
Cons (n, lazy (helper (n-1) l)) in
let rec l = lazy (helper n l) in
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
Printf.printf "%d, " content;
aux (Lazy.force next)
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 :
But I could only print compile time builded structures.
print_circular ( build_circular2 c3 ) never stops...
If you have any light about this ...
- Hi !
( Replying to myself. )
I wrote :
> Would it be possible to rewrite the above functions "circular" andThe answer is yes :
> "print_circular" with "hand made" basic lazyness i.e. in freezing
> things using unit functions ?
So we can generate circular structures without any "ref" and without calling Obj ( even indirectly by Lazy ).