on master help sign in
Merge pull request #546 from urbit/caesar-update
26607e0David Kerschner
learn/hoon/hoon-tutorial/caesar.md+57−56
@@ -31,51 +31,51 @@ demonstrates important principles that more laconic code would not. Save it as
31```
31```
32!:
32!:
33|= [msg=tape key=@ud]
33|= [msg=tape key=@ud]
34−=+ ^= caesar
34+=<
35− |%
36− ++ alpha "abcdefghijklmnopqrstuvwxyz"
37− ++ shift
38− |= [message=tape key=@ud]
39− (operate message key (encoder key))
40− ++ unshift
41− |= [message=tape key=@ud]
42− (operate message key (decoder key))
43− ++ operate
44− |= [message=tape key=@ud encoder=(map @t @t)]
45− ^- tape
46− %+ turn message
47− |= a=@t
48− (~(got by encoder) a)
49− ++ encoder
50− |= [key=@ud]
51− =/ keytape=tape (rott alpha key)
52− (coder alpha keytape)
53− ++ decoder
54− |= [key=@ud]
55− =/ keytape=tape (rott alpha key)
56− (coder keytape alpha)
57− ++ coder
58− |= [a=tape b=tape]
59− (~(put by (zipper a b)) ' ' ' ')
60− ++ zipper
61− |= [a=tape b=tape]
62− ^- (map @t @t)
63− =| chart=(map @t @t)
64− ?. =((lent a) (lent b))
65− ~| %uneven-lengths !!
66− |-
67− ?: |(?=(~ a) ?=(~ b))
68− chart
69− $(chart (~(put by chart) i.a i.b), a t.a, b t.b)
70− ++ rott
71− |= [m=tape n=@ud]
72− =/ length=@ud (lent m)
73− =+ s=(trim (mod n length) m)
74− (weld q.s p.s)
75− --
76=. msg (cass msg)
35=. msg (cass msg)
77−:- (shift.caesar msg key)
36+:- (shift msg key)
78−(unshift.caesar msg key)
37+(unshift msg key)
38+|%
39+++ alpha "abcdefghijklmnopqrstuvwxyz"
40+++ shift
41+ |= [message=tape key=@ud]
42+ (operate message key (encoder key))
43+++ unshift
44+ |= [message=tape key=@ud]
45+ (operate message key (decoder key))
46+++ operate
47+ |= [message=tape key=@ud encoder=(map @t @t)]
48+ ^- tape
49+ %+ turn message
50+ |= a=@t
51+ (~(got by encoder) a)
52+++ encoder
53+ |= [key=@ud]
54+ =/ keytape=tape (rott alpha key)
55+ (coder alpha keytape)
56+++ decoder
57+ |= [key=@ud]
58+ =/ keytape=tape (rott alpha key)
59+ (coder keytape alpha)
60+++ coder
61+ |= [a=tape b=tape]
62+ (~(put by (zipper a b)) ' ' ' ')
63+++ zipper
64+ |= [a=tape b=tape]
65+ ^- (map @t @t)
66+ =| chart=(map @t @t)
67+ ?. =((lent a) (lent b))
68+ ~| %uneven-lengths !!
69+ |-
70+ ?: |(?=(~ a) ?=(~ b))
71+ chart
72+ $(chart (~(put by chart) i.a i.b), a t.a, b t.b)
73+++ rott
74+ |= [m=tape n=@ud]
75+ =/ length=@ud (lent m)
76+ =+ s=(trim (mod n length) m)
77+ (weld q.s p.s)
78+--
79```
79```
80
80
81This generator takes two arguments: a `tape`, which is your plaintext message,
81This generator takes two arguments: a `tape`, which is your plaintext message,
@@ -124,19 +124,20 @@ event of an error.
124
124
125`|= [msg=tape key=@ud]` creates a gate that takes a cell. The head of this cell
125`|= [msg=tape key=@ud]` creates a gate that takes a cell. The head of this cell
126is a `tape`, which is a string type that's a list of `cord`s. Tapes are represented
126is a `tape`, which is a string type that's a list of `cord`s. Tapes are represented
127−as text surrounded by double-quotes, such as this: `"a tape"`. We give this input tape the face `msg`.
127+as text surrounded by double-quotes, such as this: `"a tape"`. We give this input
128−The tail of our cell is a `@ud` -- an unsigned decimal atom -- that we give the face `key`.
128+tape the face `msg`. The tail of our cell is a `@ud` -- an unsigned decimal atom
129−
129+-- that we give the face `key`.
130−`=+` adds a noun to the subject, `=^ caesar` names that noun, and `|%` is used
131−to create a core that contains the following arms. This results in having a core
132−with the face `caesar` that we can call.
133
130
134−Now let's look at the rest of the program that is outside of our core:
131+`=<` is the rune that evaluates its first child expression with respect to its
132+second child expression as the subject. In this case, we evaluate the
133+expressions in the code chunk below against the core declared later, which
134+allows us reference the core's contained arms before they are defined. Without
135+`=<`, we would need to put the code chunk below at the bottom of our program.
135
136
136```
137```
137=. msg (cass msg)
138=. msg (cass msg)
138−:- (shift.caesar msg key)
139+:- (shift msg key)
139−(unshift.caesar msg key)
140+(unshift msg key)
140```
141```
141
142
142`=. msg (cass msg)` changes the input string `msg` to lowercases. `=.` changes
143`=. msg (cass msg)` changes the input string `msg` to lowercases. `=.` changes
@@ -144,7 +145,7 @@ the leg of the subject to something else. In our case, the leg to be changed is
144`msg`, and the thing to replace it is `(cass msg)`. `cass` is a standard-library
145`msg`, and the thing to replace it is `(cass msg)`. `cass` is a standard-library
145gate that converts uppercase letters to lowercase.
146gate that converts uppercase letters to lowercase.
146
147
147−`:- (shift.caesar msg key)` and `(unshift.caesar msg key)` simply composes a
148+`:- (caesar msg key)` and `(caesar msg key)` simply composes a
148cell of a right-shifted cipher and a left-shifted cell. This is the final output
149cell of a right-shifted cipher and a left-shifted cell. This is the final output
149of our generator.
150of our generator.
150
151
@@ -295,8 +296,8 @@ by.
295`shift` is for encoding, and `unshift` is for decoding. Thus, `shift` calls the
296`shift` is for encoding, and `unshift` is for decoding. Thus, `shift` calls the
296`operate` arm with `(operate message key (encoder key))`, and `unshift` makes
297`operate` arm with `(operate message key (encoder key))`, and `unshift` makes
297that call with `(operate message key (decoder key))`. These both produce the
298that call with `(operate message key (decoder key))`. These both produce the
298−final output of the core, to be called in the form of `(shift.caesar msg key)`
299+final output of the core, to be called in the form of `(caesar msg key)`
299−and `(unshift.caesar msg key)` at the bottom of the program.
300+and `(caesar msg key)` at the bottom of the program.
300
301
301```
302```
302++ operate
303++ operate