2title = "1b: Tree Addressing"
7Check out the Nock [explanation](@/docs/nock/_index.md) for more
8information on the tree-addressing system.
14Tests whether the tree address `a` is in the head or the tail of a noun.
15Produces the constant atom `%2` if it is within the head (subtree `+2`), or
16the constant atom `%3` if it is within the tail (subtree `+3`).
54 > (cap 1) ::address '1' is in neither the head nor the tail
71Address within head/tail
73Computes the tree address of atom `a` within either the head (`+2`) or tail
95 * (add (mod a 2) (mul $(a (div a 2)) 2))
110 > (cap 5) ::`(cap a)` computes whether address `a` is in the head or the tail
128 ! exit ::address `0` is in neither the head nor the tail
131 ! exit ::address `1` is in neither the head nor the tail
140 2 3 <--here are the head (`+2`) and the tail (`+3`)
147Running `(mas 7)` in the Dojo will return `3`, because address `+3` is what
148`+7` now occupies. The tree below helps illustrate the relationship. With
149parentheses are `a` values (if `a` is in subtree `+3`), and without parentheses
150are the values returned with `(mas a)`.
153 1(3) ::new/(old) addresses
165Notice how the old values in the head (subtree `+2`) were not illustrated in
166this case, because `+7` is within the tail (subtree `+3`).
171Address within address
173Computes the absolute address of `b`, a relative address within the subtree
198 * (add (mod b 2) (mul $(b (div b 2)) 2))
223 > (cap (peg 4 2)) ::`(cap a)` computes whether address `a` is in the head or the tail
230In other words, the subtree at address `a` is treated as a tree in its own
231right (starting with root `+1`, head `+2`, and tail `+3`). Relative address
232`b` is found with respect to `a`, and then its absolute address, within the
233greater tree, is returned.
235Running `(peg 3 4)` in the Dojo, for example, will return `12`. Looking at
236a tree diagram makes it easy to see why.
243 2 3 <- here is the subtree `+3`. The subtree address is `a` in `(peg a b)`
248 8 9 10 11 12 13 14 15
249 /\ /\ /\ /\ /\ /\ /\ /\
253When we consider subtree at address `+3` by itself, it has relative addresses
254that are structured in the same way as its parent tree's absolute addresses.
255The absolute addresses are given in parentheses in the diagram below.
256Notice how relative address `+4` is at the same position as absolute address
260 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