Nock

32 thoughts
last posted Oct. 18, 2013, 4:24 p.m.

26 earlier thoughts

0

[6 [b [c d]]] is a "function" that, when applied to a, is equivalent to c if *[a b] is 0 and d if *[a b] is 1. It is therefore a basic if construct.

Much of the complexity is due to handling the case where b is neither 0 nor 1 (crashing rather than giving a bogus result that tries to apply a subtree of c or d to a).

Let's try a reduction:

*[a [6 [b [c d]]]] =>
*[a [2 [[0 1] [2 [[1 [c d]] [[1 0] [2 [[1 [2 3]] [[1 0] [4 [4 b]]]]]]]]]]]
*[*[a [0 1]] *[a [2 [[1 [c d]] [[1 0] [2 [[1 [2 3]] [[1 0] [4 [4 b]]]]]]]]]]
*[a *[a [2 [[1 [c d]] [[1 0] [2 [[1 [2 3]] [[1 0] [4 [4 b]]]]]]]]]]
*[a *[*[a [1 [c d]]] *[a [[1 0] [2 [[1 [2 3]] [[1 0] [4 [4 b]]]]]]]]]
*[a *[[c d] *[a [[1 0] [2 [[1 [2 3]] [[1 0] [4 [4 b]]]]]]]]]
*[a *[[c d] [*[a [1 0]] *[a [2 [[1 [2 3]] [[1 0] [4 [4 b]]]]]]]]]
*[a *[[c d] [0 *[a [2 [[1 [2 3]] [[1 0] [4 [4 b]]]]]]]]]
*[a *[[c d] [0 *[*[a [1 [2 3]]] *[a [[1 0] [4 [4 b]]]]]]]]
*[a *[[c d] [0 *[[2 3] *[a [[1 0] [4 [4 b]]]]]]]]
*[a *[[c d] [0 *[[2 3] [*[a [1 0]] *[a [4 [4 b]]]]]]]]
*[a *[[c d] [0 *[[2 3] [0 *[a [4 [4 b]]]]]]]]
*[a *[[c d] [0 *[[2 3] [0 +*[a [4 b]]]]]]]
*[a *[[c d] [0 *[[2 3] [0 ++*[a b]]]]]]

If *[a b] evaluates to 0 then ++*[a b] will be 2. If *[a b] evaluates to 1 then ++*[a b] will be 3.

This is then used as a tree-address into [2 3]. Because [0 2] selects the left noun of a cell, *[[2 3] [0 2]] is just 2. And because [o 3] selects the right noun of a cell, *[[2 3] [0 3]] is just 3.

This may seem like an identity operation but it insures the correct behaviour if b is neither 0 nor 1. if x is an atom greater than 3 or if x is a cell, *[[2 3] [0 x]] crashes.

We can now use the sanitized tree address into [c d] and either get c or d as appropriate without risk that some subtree of c or d will be applied to a.

5 later thoughts