[mercury-users] Self-referential data structures

Fergus Henderson fjh at cs.mu.oz.au
Fri Dec 5 17:28:16 AEDT 1997


Warwick HARVEY <warwick at cs.mu.oz.au> wrote:
>     % Provided by store module
> :- pred new_circ_elem(func(mutvar(T, S)) = T is det,
> 		      mutvar(T, S)::out,
>                       store(S)::di, store(S)::uo) is det.
> 
>     % From the module you're writing
> :- type elem(T, S) ---> elem(T, handle(T, S), handle(T, S)).
>                    % i.e. elem(Data, Prev, Next)
> :- type handle(T, S) == mutvar(elem(T, S), S).
> 
> You can write:
> 
>     ...
>     new_circ_elem(new_single_element_circular_list(Data), Ref, S0, S1),
>     ...
> 
> :- pred new_single_element_circular_list(T, handle(T, S), elem(T, S)).
> :- mode new_single_element_circular_list(in, in, out) is det.
> 
> new_single_element_circular_list(Data, Ref, Elem) :-
>     Elem = elem(Data, Ref, Ref).

Minor correction -- `new_single_element_circular_list' should be
a function, not a predicate.

I personally would just use a lambda expression, since this is
much more concise:

      new_circ_elem((func(R) = elem(Data, R, R)), Ref, S0, S1),

-- 
Fergus Henderson <fjh at cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3         |     -- the last words of T. S. Garp.



More information about the users mailing list