@@ -7,13 +7,13 @@ title: Hoon refresher / crash course
8# Basic Hoon
8# Basic Hoon
10−(This recapitulates material you've already learned in Core Hoon.
10+This recapitulates material you've already learned in Core Hoon.
11It will be a very abrupt tutorial if you don't know Hoon already,
11It will be a very abrupt tutorial if you don't know Hoon already,
12−but it may just suffice. If you don't find it obvious at first
12+but it may be enough in and of itself. If you don't find it obvious at first
13−glance, read it over again
13+glance, read it over again.
15Our goal is to get you programming interesting and useful things
15Our goal is to get you programming interesting and useful things
16−as soon as possible. To get there we have to quickly cover some
16+as soon as possible. To get there, we have to quickly cover some
17of the fundamentals of hoon. To do this we'll walk through two
17of the fundamentals of hoon. To do this we'll walk through two
18simple programs: the first [Project
18simple programs: the first [Project
19Euler](https://projecteuler.net/) problem and
19Euler](https://projecteuler.net/) problem and
@@ -23,7 +23,7 @@ To run this code, you'll need an urbit, and you'll need the
23`%examples` desk from `~zod`. If you haven't installed
23`%examples` desk from `~zod`. If you haven't installed
24urbit yet, check out the [installation
24urbit yet, check out the [installation
25instructions](http://urbit.org/docs/user/install). Once urbit is
25instructions](http://urbit.org/docs/user/install). Once urbit is
26−intalled, take a look at the [basic
26+installed, take a look at the [basic
27operation](http://urbit.org/docs/user/basic) of your urbit.
27operation](http://urbit.org/docs/user/basic) of your urbit.
29If you haven't pulled the examples desk from `~zod`, do
29If you haven't pulled the examples desk from `~zod`, do
@@ -34,8 +34,8 @@ so now:
34 [time passes...]
34 [time passes...]
35 merged with strategy %init
35 merged with strategy %init
37−The merge could take several minutes; you'll know it's done when
37+This merge could take several minutes; you'll know it's done when
38−"merged with strategy %init" is printed. Mount the new files to
38+"merged with strategy %init" is printed. Next, mount the new files to
39your Unix pier directory:
39your Unix pier directory:
41 ~fintud-macrep:dojo> |mount /=examples=
41 ~fintud-macrep:dojo> |mount /=examples=
@@ -66,6 +66,7 @@ under `/examples/gen/euler1.hoon`):
68:: project euler 1
68:: project euler 1
69:: https://projecteuler.net/problem=1
70:: https://projecteuler.net/problem=1
70:: run in dojo with +euler1
71:: run in dojo with +euler1
@@ -75,20 +76,20 @@ under `/examples/gen/euler1.hoon`):
76=< (sum 1.000)
77=< (sum 1.000)
78−:::: ~fintud-macrep
79+:::: ~sivtyv-barnel
85 ?: (lth a b)
86 ?: (lth a b)
87 (add b $(b (add 3 b)))
88 (add b $(b (add 3 b)))
93 ?: (lte a b)
94 ?: (lte a b)
@@ -97,17 +98,19 @@ under `/examples/gen/euler1.hoon`):
97 (add b $(b (add b 5)))
98 (add b $(b (add b 5)))
101 (add (five a) (three a))
102 (add (five a) (three a))
106+<blockquote class=“blockquote”>
105> Hoon is not generally whitespace sensitive, but we do have two
107> Hoon is not generally whitespace sensitive, but we do have two
106> different kinds of whitespace: a single space and a gap, which
108> different kinds of whitespace: a single space and a gap, which
107> is two or more spaces or a linebreak. Tabs are taboo. Do not
109> is two or more spaces or a linebreak. Tabs are taboo. Do not
108> use them. Really. For a more detailed explanation of when to
110> use them. Really. For a more detailed explanation of when to
109> use spaces vs. gaps, see the syntax section before the first
111> use spaces vs. gaps, see the syntax section before the first
110> excercises.
112> excercises.
113+<blockquote class=“blockquote”>
112### Lines 1-11:
115### Lines 1-11:
@@ -123,11 +126,11 @@ more detail later.
124### How to form expressions
127### How to form expressions
126−Hoon does not use reserved words to form expressions. Instead,
129+Hoon does not use reserved words to form expressions. (We did recently implement an alternative keyword syntax, which is not covered here. For more information, see the [syntax section]()). Instead,
127−expressions are formed with runes, which are diagraphs of two
130+expressions are formed with runes--diagraphs of two
128ascii symbols. Each rune takes a specific number of
131ascii symbols. Each rune takes a specific number of
129children--either expressions formed by other runes or literals
132children--either expressions formed by other runes or literals
130−that produce their own value.
133+that produce their own value (some runes take n children, and are usually closed with `==`).
132For example, the rune `?:` from line 17 is the classic
135For example, the rune `?:` from line 17 is the classic
133'if-then-else' statement, and thus takes three children:
136'if-then-else' statement, and thus takes three children:
@@ -182,7 +185,7 @@ Let's step into each of the three arms within our core.
182### `++ sum`
185### `++ sum`
186 (add (five a) (three a))
189 (add (five a) (three a))
@@ -201,8 +204,8 @@ Let's step into each of the three arms within our core.
201### ++ three
204### ++ three
203 ++ three
206 ++ three
207 ?: (lth a b)
210 ?: (lth a b)
@@ -263,7 +266,7 @@ refer to our cheatsheat at the bottom.
266−|% start core (collection of named ++ arms)
269+|% start core (collection of named ++ arms; an "object")
267|= define function
270|= define function
268=| define variable from type with default value
271=| define variable from type with default value
269|- drop a recursion point
272|- drop a recursion point
@@ -274,14 +277,6 @@ Review
275New material
278New material
277−- :- make a cell of values. The irregular wide form of this is
278− [a b] with two expressions separated by a single space.
280−- Cords are one datatype for text in hoon. They're just a big
281− atom formed from adjacent unicode bytes -- a "c string". To
282− produce a cord enclose text within single quotes. To set the type
283− of an argument to a cord, use @t.
285- There are two syntaxes for writing Hoon: tall form and wide
280- There are two syntaxes for writing Hoon: tall form and wide
@@ -304,109 +299,15 @@ New material
304 =((mod b 3) 0). In this case = is actually an irregular form
299 =((mod b 3) 0). In this case = is actually an irregular form
305 of .=, which tests its two children for equality.
300 of .=, which tests its two children for equality.
307− Another irregular form is [a b] for :-(a b)
309 Surrounding a function with () is an irregular wide form
302 Surrounding a function with () is an irregular wide form
310− syntax for calling a function with n arguments.
303+ syntax for calling a function with n arguments. More on this later.
316−Now we're going to cover the boiler plate that we skimmed over
321− =< (sum [1.000 2.000])
323−This program is a cell of two elements: the first, `%say`, tells
324−the interpreter what to produce--in this case a value.
326−The second element is `|=`, which we know produces a function.
327−`|=`'s first child is its argument(s), which in this case is any
328−noun (`*`). Its second child is the remainder of the program.
330−Similarly, the rest of the program is a cell of the literal
331−`%noun`, which tells the shell that we're producing a value of
332−type `noun`, and the second child contains the code that we run
333−to actually produce our value of the type `noun`.
335−`=<` is a rune that takes two children. The second child is the
336−context against which we run the first child. So in this case, we
337−are running the expression `(sum 1.000)` against everything
338−contained within the `|%`. In Hoon, we call the code executed the
339−"formula" and its context the "subject".
342−::::::::::::::::::::::::::::::
343−=< (sum 1.000) :: formula
344−::::::::::::::::::::::::::::::
352− (add b $(b (add 3 b))) ::
360− ?: =((mod b 3) 0) ::
362− (add b $(b (add b 5))) ::
366− (add (five a) (three a)) ::
368−::::::::::::::::::::::::::::::
371−In nearly every language there is a similar concept of a
372−"context" in which expressions are executed. For example, in C
373−this includes things like the call stack, stack variables, and so
376−Hoon is unique in that this context is a first-class value.
305+- :- makes a cell of values. The irregular wide form of this is
377−Scheme allows a sort of reification of the context through
306+ [a b], with two expressions separated by a single space. (While the regular form of this rune takes a fixed number of children--2--its irregular wide form can accept n expressions:[a1..an])
378−continutations, and some may see a parallel to Forth's stack, but
379−Hoon takes takes the concept one step further.
381−Our starting subject is the standard library, which is defined in
308+- Cords are one datatype for text in hoon. They're just a big
382−`/arvo/hoon.hoon` and `/arvo/zuse.hoon`. This is where functions
309+ atom formed from adjacent unicode bytes -- a "c string". To
383−like `add` are defined. When we define a core with `|%`, we
310+ produce a cord enclose text within single quotes. To set the type
384−don't throw away the subject (i.e. the standard library); rather,
311+ of an argument to a cord, use @t.
385−we stack the new core on top of the old subject so that both are
390−- Pass `++sum` its arguments (`2000` and `3000`) from the
313+```
393−- Comment out all of the arms of the `|%`. Now add another arm
394− and call it `++add`, have it accept two arguments and procduce
395− 42 (regardless of input). Change the `=<` line to `[(add 5 7)
396− (^add 5 7)]`. Can you recognize what's happening?
400−- To pass arguments from the command line to a program, you
401− replace the `*` in the first line of the boiler plate to
402− `[^ [[arg=TYPE ~] ~]]` where `TYPE` is replaced with the
403− type of argument you're expecting. Then `+euler1 a` from
404− the dojo sets `arg` to `a`.
405−- The empty list is `~`
406−- Lisp-style cons (construct a cell/prepend an element) is
407− `[new-element list]`
408−- For example, the first three positive integers are `[1 2 3
410−- `gte` tests whether `a` is greater than or equal to `b`.
411−- `mod` runs the modulo operation on two atoms.
412−- See the [basic math section]() for more info.