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