2title = "1b: Tree Addressing"
7Check out the Nock [explanation](/docs/nock/) for more
8information on the tree-addressing system.
14Tests whether the tree address `a` is in the head or the tail of a [`noun`](/docs/glossary/noun).
15Produces the constant [`atom`](/docs/glossary/atom) `%2` if it is within the head (subtree `+2`), or
16the constant [`atom`](/docs/glossary/atom) `%3` if it is within the tail (subtree `+3`).
20`a` is an [`atom`](/docs/glossary/atom).
24A constant [`atom`](/docs/glossary/atom).
59> (cap 1) ::address '1' is in neither the head nor the tail
81Address within head/tail
83Computes the tree address of [`atom`](/docs/glossary/atom) `a` within either the head (`+2`) or tail
84(`+3`) of a [`noun`](/docs/glossary/noun).
88`a` is an [`atom`](/docs/glossary/atom).
92An [`atom`](/docs/glossary/atom).
104 * (add (mod a 2) (mul $(a (div a 2)) 2))
123> (cap 5) ::`(cap a)` computes whether address `a` is in the head or the tail
150> (mas 0) ::address `0` is in neither the head nor the tail
155> (mas 1) ::address `1` is in neither the head nor the tail
165 2 3 <--here are the head (`+2`) and the tail (`+3`)
172Running `(mas 7)` in the [`Dojo`](/docs/glossary/dojo) will return `3`, because address `+3` is what
173`+7` now occupies. The tree below helps illustrate the relationship. With
174parentheses are `a` values (if `a` is in subtree `+3`), and without parentheses
175are the values returned with `(mas a)`.
178 1(3) ::new/(old) addresses
190Notice how the old values in the head (subtree `+2`) were not illustrated in
191this case, because `+7` is within the tail (subtree `+3`).
197Address within address
199Computes the absolute address of `b`, a relative address within the subtree
204`a` is an [`atom`](/docs/glossary/atom).
206`b` is an [`atom`](/docs/glossary/atom).
210An [`atom`](/docs/glossary/atom).
224 * (add (mod b 2) (mul $(b (div b 2)) 2))
261> (cap (peg 4 2)) ::`(cap a)` computes whether address `a` is in the head or the tail
267In other words, the subtree at address `a` is treated as a tree in its own
268right (starting with root `+1`, head `+2`, and tail `+3`). Relative address
269`b` is found with respect to `a`, and then its absolute address, within the
270greater tree, is returned.
272Running `(peg 3 4)` in the [`Dojo`](/docs/glossary/dojo), for example, will return `12`. Looking at
273a tree diagram makes it easy to see why.
280 2 3 <- here is the subtree `+3`. The subtree address is `a` in `(peg a b)`
285 8 9 10 11 12 13 14 15
286 /\ /\ /\ /\ /\ /\ /\ /\
290When we consider subtree at address `+3` by itself, it has relative addresses
291that are structured in the same way as its parent tree's absolute addresses.
292The absolute addresses are given in parentheses in the diagram below.
293Notice how relative address `+4` is at the same position as absolute address
297 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