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