File: tree-copy

package info (click to toggle)
scheme9 2025.08.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,080 kB
  • sloc: lisp: 16,752; ansic: 11,869; sh: 806; makefile: 237; sed: 6
file content (16 lines) | stat: -rw-r--r-- 576 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
S9 LIB  (tree-copy pair)              ==>  pair
        (tree-copy pair 'with-atoms)  ==>  pair

Create an exact copy of an arbitrary non-cyclic cons structure.
When a second argument is passed to TREE-COPY and that argument
is not #F, then TREE-COPY will copy modifiable leaves of the tree,
too.

(tree-copy '(((a . b) (c . d)) (e . f)))
    ==>  (((a . b) (c . d)) (e . f))

(let* ((tree  (list (string #\A)))
       (tree2 (tree-copy tree))
       (tree3 (tree-copy tree 'with-atoms)))
  (string-set! (car tree) 0 #\X)
  (list tree2 tree3))              ==>  (("X") ("A"))