2title = "1b: Tree Addressing"
6Check out the Nock [explanation](@/docs/learn/nock/_index.md) for more
7information on the tree-addressing system.
13Tests whether the tree address `a` is in the head or the tail of a noun.
14Produces the constant atom `%2` if it is within the head (subtree `+2`), or
15the constant atom `%3` if it is within the tail (subtree `+3`).
53 > (cap 1) ::address '1' is in neither the head nor the tail
70Address within head/tail
72Computes the tree address of atom `a` within either the head (`+2`) or tail
94 * (add (mod a 2) (mul $(a (div a 2)) 2))
109 > (cap 5) ::`(cap a)` computes whether address `a` is in the head or the tail
127 ! exit ::address `0` is in neither the head nor the tail
130 ! exit ::address `1` is in neither the head nor the tail
139 2 3 <--here are the head (`+2`) and the tail (`+3`)
146Running `(mas 7)` in the Dojo will return `3`, because address `+3` is what
147`+7` now occupies. The tree below helps illustrate the relationship. With
148parentheses are `a` values (if `a` is in subtree `+3`), and without parentheses
149are the values returned with `(mas a)`.
152 1(3) ::new/(old) addresses
164Notice how the old values in the head (subtree `+2`) were not illustrated in
165this case, because `+7` is within the tail (subtree `+3`).
170Address within address
172Computes the absolute address of `b`, a relative address within the subtree
197 * (add (mod b 2) (mul $(b (div b 2)) 2))
222 > (cap (peg 4 2)) ::`(cap a)` computes whether address `a` is in the head or the tail
229In other words, the subtree at address `a` is treated as a tree in its own
230right (starting with root `+1`, head `+2`, and tail `+3`). Relative address
231`b` is found with respect to `a`, and then its absolute address, within the
232greater tree, is returned.
234Running `(peg 3 4)` in the Dojo, for example, will return `12`. Looking at
235a tree diagram makes it easy to see why.
242 2 3 <- here is the subtree `+3`. The subtree address is `a` in `(peg a b)`
247 8 9 10 11 12 13 14 15
248 /\ /\ /\ /\ /\ /\ /\ /\
252When we consider subtree at address `+3` by itself, it has relative addresses
253that are structured in the same way as its parent tree's absolute addresses.
254The absolute addresses are given in parentheses in the diagram below.
255Notice how relative address `+4` is at the same position as absolute address
259 1(3) ::new/(old) addresses
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list