@@ -1,267 +0,0 @@
1−---
2−date: ~2016.7.16
3−type: post
4−title: Urbyte 0: setup, nouns
5−author: The Urbit Team
6−layout: urbit,post
7−comments: true
8−hide: true
9−navmode: navbar
10−navdpad: false
11−navselect: blog
12−navpath: /
13−navhome: /
14−navclass: urbit
15−---
16−<br /><br />
17−This is the first post in our new Urbyte series.
18−
19−The Urbyte concept is that you, an ordinary programmer, not a
20−kernel hacker or a Haskell magician (not that there's anything
21−wrong with kernel hackers or Haskell magicians) can learn Hoon,
22−Urbit's programming language.
23−
24−We're going to test this daring idea by teaching you Hoon, in
25−easily digestible, byte-size chunks. Each post in the "Urbytes"
26−series is designed to take about 15 minutes to consume. (Maybe
27−10, if you're a kernel hacker or a Haskell magician.)
28−
29−We'll assume no knowledge of Urbit except for previous lessons.
30−Ideally you have some basic familiarity with programming and can
31−operate the Unix command line.
32−
33−We strongly encourage you to type in all the examples in each
34−Urbyte. Urbytes are short, but not meant to be skimmed.
35−
36−## Install
37−
38−First, [install Urbit](http://urbit.org/docs/using/install/) on any
39−Mac or Unix machine. On Windows, make a virtual Linux machine
40−using VirtualBox or a similar tool.
41−
42−While you're downloading, read about Urbit ships:
43−
44−## Ships
45−
46−In the Urbyte exercises you'll be running what we call a "fake
47−`~zod`." A string starting with `~`, like `~zod`, is what Urbit
48−calls a *ship*.
49−
50−A ship is one number which means four things: a network address,
51−a cryptographic identity, a memorable name, and a single virtual
52−computer. We write this number in a base-256 format, where each
53−digit is a syllable.
54−
55−`~zod` is ship zero, making it a very important ship indeed.
56−Your "fakezod" thinks it's `~zod`, but no one can talk to it.
57−
58−This is not the true Urbit experience, of course. But it's
59−ideal if all you want to do is play around with Hoon. If you
60−have a real urbit ship, of course, you can use that instead.
61−
62−## Create and restart
63−
64−To create a fakezod, run:
65−
66−```
67−$ urbit -FI ~zod -c myzod
68−```
69−
70−This will take about 30 seconds and spin out a bunch of boot
71−messages. At the end, you'll see:
72−
73−```
74−ames: on localhost, UDP 31337.
75−http: live (insecure, public) on 8080
76−http: live ("secure", public) on 8443
77−http: live (insecure, loopback) on 12321
78−~zod:dojo>
79−```
80−
81−To make sure everything is working, type `(add 2 2)` at the
82−prompt, then hit return. Your screen now shows:
83−
84−```
85−ames: on localhost, UDP 31337.
86−http: live (insecure, public) on 8080
87−http: live ("secure", public) on 8443
88−http: live (insecure, loopback) on 12321
89−> (add 2 2)
90−4
91−~zod:dojo>
92−```
93−
94−That's super awesome! You made your first noun! We'll make a
95−few more in a second. First, quit Urbit with `^D`:
96−
97−```
98−> (add 2 2)
99−4
100−~zod:dojo>
101−$
102−```
103−
104−You now have an Urbit image, or *pier*, in the `myzod` directory.
105−Right now the only thing in your pier is Urbit's system files:
106−
107−```
108−$ ls -a myzod
109−./ ../ .urb/
110−```
111−
112−Restarting uses almost the same command, but without `-c`. There
113−are also fewer startup messages.
114−
115−```
116−$ urbit -FI ~zod myzod
117−[...]
118−ames: on localhost, UDP 31337.
119−http: live (insecure, public) on 8080
120−http: live ("secure", public) on 8443
121−http: live (insecure, loopback) on 12321
122−~zod:dojo>
123−```
124−
125−Note also that Urbit is an ACID database. You can stop it
126−politely with `^D`, abruptly with `^Z`, even violently with `kill
127−-9`. None of these should cause Urbit to lose data. If you do
128−manage to corrupt a pier, it's a bug; please let us know.
129−
130−## Let's make a noun
131−
132−We already made a noun. Let's make another. (We won't show the
133−`~zod:dojo> ` prompt in future, just the echoed command.)
134−
135−```
136−~zod:dojo> 42
137−```
138−
139−You'll see:
140−
141−```
142−> 42
143−42
144−~zod:dojo>
145−```
146−
147−## Understanding nouns
148−
149−A value in Urbit is called a *noun*. A noun is either an *atom*
150−(an unsigned integer of any size) or a *cell* (an ordered pair of
151−any two nouns).
152−
153−In a sentence, a noun is a binary tree whose leaves are numbers.
154−If you know Lisp, a noun is a minimalist S-expression. A noun
155−can't have cycles (pointer loops). An implementation can use
156−dags (shared subtrees), but the programmer can't detect them
157−(there's no pointer equality test).
158−
159−The absence of graph cycles makes nouns much easier to save in
160−persistent storage, send over the network, manage memory for
161−(Urbit has no tracing garbage collector), etc.
162−
163−## Let's make some atoms
164−
165−So one kind of atom is a number. And the syntax (or at least one
166−syntax) for a number is a decimal, like `42`. Seems legit.
167−
168−But just to be sure we understand this decimal thing, let's make
169−some atoms:
170−
171−```
172−> 4
173−4
174−```
175−
176−Seems pretty straightforward.
177−
178−```
179−> 42
180−42
181−```
182−
183−Why not try it again to make sure?
184−
185−```
186−> 420
187−420
188−```
189−
190−Can we add another zero?
191−
192−```
193−~zod:dojo> 420
194−```
195−
196−Wat? When you tried to type that next zero and turn `420` into
197−`4200`, Urbit actually *deleted the `0` and beeped at you.*
198−
199−But isn't an atom an unsigned integer of any size? It is.
200−But the syntax is actually `4.200`:
201−
202−```
203−> 4.200
204−4.200
205−> (mul 42 100)
206−4.200
207−```
208−
209−Just think of it as "4,200", written the German way with a dot.
210−
211−Why? Long decimals are unreadable. Human beings know this, so
212−we group them in threes. For some historical reason, programming
213−languages never copied this marvelous usability innovation.
214−
215−English notation for decimals is more common than German
216−notation. Unfortunately, dot is URL-safe and comma isn't, and
217−it's nice to have a regular syntax for atoms that's URL-safe.
218−
219−As for why Urbit deleted your zero: it parses your command line
220−as you type, and rejects any characters after the parser stops.
221−
222−## Let's make some cells
223−
224−There's not much mystery about cells. A cell has a head and a
225−tail, and puts brackets around them:
226−
227−```
228−> [42 420]
229−[42 420]
230−```
231−
232−`42` is the *head* and `420` is the *tail* of the cell. Cell
233−syntax nests to the right:
234−
235−```
236−> [4 [42 420]]
237−[4 42 420]
238−
239−> [4 42 420]
240−[4 42 420]
241−
242−> [[4 42] 420]
243−[[4 42] 420]
244−```
245−
246−If you look at these nouns as binary trees, you see
247−
248−```
249− [4 [42 420]] [[4 42] 420]
250− . .
251− / \ / \
252− 4 . . 420
253− / \ / \
254− 42 420 4 42
255−```
256−
257−Each number is an atom; each dot is a cell.
258−
259−## Questions and/or exercises
260−
261−And we're done with our very first Urbyte! Here's a question to
262−think about. These exercises are never required, but they may
263−be fun and/or interesting.
264−
265−The question today is: what is the right way to represent common
266−data structures as nouns? A linked list? A string? A *signed*
267−integer? A table or associative array?