on 8564483 help sign in
urbit.org/docs/arvo/subject.md
105 lines 3.6 kB history raw
1---
2navhome: /docs
3sort: 2
4next: true
5title: The subject
6---
8# The subject
10Now we're going to cover the boiler plate that we skimmed over
11earlier.
13 :- %say |= *
14 :- %noun
15 =< (sum [1.000 2.000])
17This program is a cell of two elements: the first, `%say`, tells
18the interpreter what to produce--in this case a value.
20The second element is `|=`, which we know produces a function.
21`|=`'s first child is its argument(s), which in this case is any
22noun (`*`). Its second child is the remainder of the program.
24Similarly, the rest of the program is a cell of the literal
25`%noun`, which tells the shell that we're producing a value of
26type `noun`, and the code that we run to actually produce our value of the type `noun`.
28`=<` is a rune that takes two children. The second child is the
29context against which we run the first child. So in this case, we
30are running the expression `(sum 1.000)` against everything
31contained within the `|%`. In Hoon, we call the code executed the
32"formula" and its context the "subject".
34```
35::::::::::::::::::::::::::::::
36=< (sum 1.000) :: formula
37::::::::::::::::::::::::::::::
38|% ::
39++ three ::
40 |= a/@ ::
41 =| b/@ ::
42 |- ^- @u ::
43 ?: (lth a b) ::
44 0 ::
45 (add b $(b (add 3 b))) ::
46 ::
47++ five ::
48 |= a/@ :: subject
49 =| b/@ ::
50 |- ^- @ ::
51 ?: (lte a b) ::
52 0 ::
53 ?: =((mod b 3) 0) ::
54 $(b (add b 5)) ::
55 (add b $(b (add b 5))) ::
56 ::
57++ sum ::
58 |= a/@u ::
59 (add (five a) (three a)) ::
60-- ::
61::::::::::::::::::::::::::::::
62```
64In nearly every language there is a similar concept of a
65"context" in which expressions are executed. For example, in C
66this includes things like the call stack, stack variables, and so
67on.
69Hoon is unique in that this context is a first-class value.
70Scheme allows a sort of reification of the context through
71continutations, and some may see a parallel to Forth's stack, but
72Hoon takes takes the concept one step further.
74Our starting subject is the standard library, which is defined in
75`/arvo/hoon.hoon` and `/arvo/zuse.hoon`. This is where functions
76like `add` are defined. When we define a core with `|%`, we
77don't throw away the subject (i.e. the standard library); rather,
78we stack the new core on top of the old subject so that both are
79accessible.
81**Exercises**:
83- Pass `++sum` its arguments (`2000` and `3000`) from the
84 commandline.
86- Comment out all of the arms of the `|%`. Now add another arm
87 and call it `++add`, have it accept two arguments and procduce
88 42 (regardless of input). Change the `=<` line to `[(add 5 7)
89 (^add 5 7)]`. Can you recognize what's happening?
91Cheatsheet:
93- To pass arguments from the command line to a program, you
94 replace the `*` in the first line of the boiler plate to
95 `{^ {{arg/TYPE ~} ~}}` where `TYPE` is replaced with the
96 type of argument you're expecting. Then `+euler1 a` from
97 the dojo sets `arg` to `a`.
98- The empty list is `~`
99- Lisp-style cons (construct a cell/prepend an element) is
100 `[new-element list]`
101- For example, the first three positive integers are `[1 2 3
102 ~]`
103- `gte` tests whether `a` is greater than or equal to `b`.
104- `mod` runs the modulo operation on two atoms.
105- See the basic math section in `/arvo/hoon.hoon` for more info.