5title: Hoon refresher / crash course
10This recapitulates material you've already learned in Core Hoon.
11It will be a very abrupt tutorial if you don't know Hoon already,
12but it may be enough in and of itself. If you don't find it obvious at first
13glance, read it over again.
15Our goal is to get you programming interesting and useful things
16as 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
18simple programs: the first [Project
19Euler](https://projecteuler.net/) problem and
20[fizzbuzz](https://en.wikipedia.org/wiki/Fizz_buzz).
22To run this code, you'll need an urbit, and you'll need the
23`%examples` desk from `~zod`. If you haven't installed
24urbit yet, check out the [installation
25instructions](http://urbit.org/docs/user/install). Once urbit is
26installed, take a look at the [basic
27operation](http://urbit.org/docs/user/basic) of your urbit.
29If you haven't pulled the examples desk from `~zod`, do
32 ~fintud-macrep:dojo> |merge %examples ~zod %examples
35 merged with strategy %init
37This merge could take several minutes; you'll know it's done when
38"merged with strategy %init" is printed. Next, mount the new files to
39your Unix pier directory:
41 ~fintud-macrep:dojo> |mount /=examples=
43Switch desks to run commands from the `%examples` desk:
45 ~fintud-macrep:dojo> =dir /=examples=
46 =% /~fintud-macrep/examples/~2015.11.13..02.25.00..41e9/
50 ~fintud-macrep:dojo> +euler1
55Let's check out the code for Euler 1. First, the problem:
58If we list all the natural numbers below 10 that are multiples of
593 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
61Find the sum of all the multiples of 3 or 5 below 1000.
64Here is the hoon solution (which should be in your pier directory
65under `/examples/gen/euler1.hoon`):
70:: https://projecteuler.net/problem=1
71:: run in dojo with +euler1
88 (add b $(b (add 3 b)))
98 (add b $(b (add b 5)))
102 (add (five a) (three a))
106<blockquote class=“blockquote”>
107> Hoon is not generally whitespace sensitive, but we do have two
108> different kinds of whitespace: a single space and a gap, which
109> is two or more spaces or a linebreak. Tabs are taboo. Do not
110> use them. Really. For a more detailed explanation of when to
111> use spaces vs. gaps, see the syntax section before the first
113<blockquote class=“blockquote”>
117Any line that begins with `::` is a comment.
123All you need to know about the lines above is that they call the
124`++sum` function with an argument of `1.000`. We'll cover them in
127### How to form expressions
129Hoon 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,
130expressions are formed with runes--diagraphs of two
131ascii symbols. Each rune takes a specific number of
132children--either expressions formed by other runes or literals
133that produce their own value (some runes take n children, and are usually closed with `==`).
135For example, the rune `?:` from line 17 is the classic
136'if-then-else' statement, and thus takes three children:
138 ?: (lth a b) :: if first child evals to true
139 0 :: then produce result of second
140 (add b $(b (add 3 b))) :: else, produce result of third
142Since runes are such a fundamental structure in Hoon, we found
143ourselves speaking them out loud frequently. It quickly grew
144cumbersome to have to say "question mark, colon" to describe
145`?:`. To alleviate this problem, we came up with our own naming
146scheme: each ascii glyph has a single syllable pronunciation
147phonetically designed to be both easily remembered and easily
148pronounced in conjunction with the other glyphs (when forming a
151See the entire naming schema below/or link to it:
154 ace [1 space] gal < pel (
155 bar | gap [>1 space, nl] per )
167Using our naming scheme `?:` is said 'wut col'.
171Now let's quickly walk through this code line-by-line. Lines
17212-34 are wrapped in a `|%` (pronounced 'bar cen'), which
173produces a core, a fundamental datatype in hoon similar to a
174struct, class, or object. A core is just a map of names
175to any kind of code, whether it be functions or data. Each
176element in this map begins with a `++` followed by the name and
177the corresponding code. Since `|%` takes an arbitrary number of
178children, it needs to be closed with a `--`.
180> `++` is not technically a rune, since it is only used in core
181> syntax as shown above
183Let's step into each of the three arms within our core.
189 (add (five a) (three a))
192`|=` produces a function, much like a lambda in lisp. It takes two children:
194- A set of argument(s). In this case our argument set only
195 contains one: `a` which is required to be an atom or natural
196 number, denoted by `@`.
198- The body of the function itself, which is executed when the
199 function is called (in this case, with `(sum 1.000)`). This
200 particular function adds the results of evaluating the gates `++
201 five` and `++three` with each of their respective input
202 parameters set to `a`.
212 (add b $(b (add 3 b)))
214As above, `++three` takes an integer argument, `a`, and then
215executes the remainder of the code with `a` set to the actual
218Similarly, `=|` pushes its first child, `b` into our context (in
219other words, it declares a variable `b`) and executes the
220remainder of the code. However, `b` is not an argument; `=|`
221sets `b` to the default value of whatever type it is declared as.
222Since the default value of an atom is `0`, b is set to `0`.
224So now we have two variables: `a` is set to our input, and `b` is
227The easiest way to think about `|-` that it lays down a recursion
228point. More on this later.
230`^-` is just a cast that sets the result of the remainder of the
231code to an unsigned integer, `@u`.
233In pseudocode, the last three lines read like this: if `a` is
234less than `b`, produce zero. Else, add `b` to the result of
235rerunning the segment of the function following the `|-` with the
236value of `b` changed to `b` plus three.
238The only thing that should look completely unfamiliar to you here
239is the `$(b (add 3 b))`, which causes us to recurse back to our
240last recursion point with the value of `b` set to `(add 3 b)`.
241Note that we only specify what changes (`b` in this case). If
242you recurse by an actual function call, then you have to specify
245> If you're familiar with Clojure, `|-` is `loop` and `$()` is
250Please tweak your code to complete the following excercises.
252There are a few runes and some syntax that we have yet to cover that
253you will need to complete the excercises below. For these, please
254refer to our cheatsheat at the bottom.
256- Read and understand `++five` line by line.
258- Change `++sum` to accept two variables, `a` and `b`. Pass `a`
259 to three and `b` to five. Then run the code with `a` set to
260 `1.000` and b set to `2.000`.
262- Check if this new result is under one thousand. If it is,
263 return the string 'result is less than one thousand'. If not,
264 return 'result is greater than or equal to one thousand'.
269|% start core (collection of named ++ arms; an "object")
271=| define variable from type with default value
272|- drop a recursion point
276(function args ...) call function
280- There are two syntaxes for writing Hoon: tall form and wide
283 In tall form, expressions are formed with either two spaces or
284 a line break separating both a rune from its children and each
285 of its children from one another. We use tall form when writing
286 multiline expressions.
288 For more concise expressions, we use wideform, which is always
289 a single line. Wideform can be used inside tall form
290 expressions, but not vice versa.
292 Wideform expressions are formed with a rune followed by ()
293 containing its children, all of which are separated by a
294 single space. For example to make a cell of two elements:
298 We've already seen wideform in action, for example with
299 =((mod b 3) 0). In this case = is actually an irregular form
300 of .=, which tests its two children for equality.
302 Surrounding a function with () is an irregular wide form
303 syntax for calling a function with n arguments. More on this later.
305- :- makes a cell of values. The irregular wide form of this is
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])
308- Cords are one datatype for text in hoon. They're just a big
309 atom formed from adjacent unicode bytes -- a "c string". To
310 produce a cord enclose text within single quotes. To set the type
311 of an argument to a cord, use @t.
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list