"Accessing Environmental Variables Through Integers" hard to understand what it means

I’ve been reading this chapter for two days, and I still don’t understand why integers can access data in a way that we can’t understand!

Thank having a forum to communicate with.


In the above examples we only used 1 which access the root of the tree and returns the entire solution list.

$ brun '1' '("example" "data" "for" "test")'("example" "data" "for" "test")

However, every unquoted integer in the lower level language refers to a part of the solution.

You can imagine a binary tree of f and r , where each node is numbered:

              1
             / \
            /   \
           /     \
          /       \
         /         \
        /           \
       2             3
      / \           / \
     /   \         /   \
    4      6      5     7
   / \    / \    / \   / \
  8   12 10  14 9  13 11  15

etc.
$ brun '2' '("example" "data" "for" "test")'"example"
$ brun '3' '("example" "data" "for" "test")'("data" "for" "test")
$ brun '5' '("example" "data" "for" "test")'"data"

And this is designed to work when there are lists inside lists too.

$ brun '4' '(("deeper" "example") "data" "for" "test")'"deeper"
$ brun '5' '(("deeper" "example") "data" "for" "test")'"data"
$ brun '6' '(("deeper" "example") "data" "for" "test")'("example")
1 Like

Basically, the left child will add f to the beginning while the right child will add r to it.

Let’s try your example above:
("example" "data" "for" "test")

2 is (f 1), so the result is "example".
3 is (r 1), so the result is ("data" "for" "test").
5 is (f (r 1)), so the result is, (f ("data" "for" "test")) which is "data".

Let’s try another example:
(("deeper" "example") "data" "for" "test")

4 is (f (f 1)), so the result is (f ("deeper" "example")) which is "deeper".
5 is (f (r 1)), so the result is (f ("data" "for" "test")) which is "data".
6 is (r (f 1)), so the result is (r ("deeper" "example")) which is ("example")

Let’s try to reformat the example above to a tree too:

                (("deeper" "example") "data" "for" "test")
                /                                        \
    ("deeper" "example")                            ("data" "for" "test")
    /                   \                           /                   \
"deeper"            ("example")                 "data"              ("for" "test")

                    1

            /               \

         (f 1)             (r 1)

       /       \          /      \

   (f (f 1)) (r (f 1)) (f (r 1)) (r (r 1))

This has a neat trick to calculate the number from the position of the value that you want.

Hope this helps!

4 Likes

After changing the formula according to your example, I finally understand why there is this result.

Thank You

3 Likes