Task

Demonstrate how to copy data structures containing complex heterogeneous and cyclic semantics.

This is often referred to as deep copying, and is normally required where structures are mutable and to ensure that independent copies can be manipulated without side-effects.

If this facility is not built into the language, it is permissible to use functions from a common library, or a coded procedure.



The task should show:

 Relevant semantics of structures, such as their homogeneous or heterogeneous properties, or containment of (self- or mutual-reference) cycles.
 Any limitations of the method.
 That the structure and its copy are different.
 Suitable links to external documentation for common libraries.


list L1, L2;
 
# Lists are heterogeneous:
l_append(L1, 3);
l_append(L1, "deep");
 
# and may contain self references.
# A self references in the last position:
l_link(L1, -1, L1);
 
# List may also contain mutual references.
# Create a new list in the last position:
l_n_list(L1, -1);
# Add a reference to the top level list to the nested list:
l_link(l_q_list(L1, -1), -1, L1);
 
# There are no limitations to the deep copy method:
l_copy(L2, L1);
 
# Modify the string in the original list,
# via the self reference in the 3rd position
l_r_text(l_q_list(L1, 2), 1, "copy");
 
# Show the string in the two lists:
o_text(l_query(L2, 1));
o_text(l_query(L1, 1));
o_byte('\n');
 
# And again, via the included self references:
o_text(l_query(l_query(L2, 2), 1));
o_text(l_query(l_query(L1, 2), 1));
o_byte('\n');