Method: Cons#copy_tree
- Defined in:
- lib/cons.rb
#copy_tree ⇒ Object Also known as: tree_copy
Creates a copy of a tree of conses.
If tree is not a cons, it is returned; otherwise, the result is a new cons of the results of calling copy-tree on the car and cdr of tree. In other words, all conses in the tree represented by tree are copied recursively, stopping only when non-conses are encountered.
copy-tree does not preserve circularities and the sharing of substructure.
241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/cons.rb', line 241 def copy_tree new_car = if car.kind_of? Cons car.copy_tree else car end new_cdr = if cdr.kind_of? Cons cdr.copy_tree else cdr end Cons[new_car,new_cdr] end |