on c19b089 help sign in
437 lines 6.5 kB history raw
1+++
2title = "2f: Noun Ordering"
3weight = 9
4template = "doc.html"
5+++
7### `++aor`
9Alphabetical order
11Computes whether `a` and `b` are in alphabetical order, producing a flag.
13#### Accepts
15`a` is a noun.
17`b` is a noun.
19#### Produces
21A flag.
23#### Source
25```hoon
26 ++ aor
27 ~/ %aor
28 |= [a=* b=*]
29 ^- ?
30 ?: =(a b) &
31 ?. ?=(@ a)
32 ?: ?=(@ b) |
33 ?: =(-.a -.b)
34 $(a +.a, b +.b)
35 $(a -.a, b -.b)
36 ?. ?=(@ b) &
37 |-
38 =+ [c=(end 3 1 a) d=(end 3 1 b)]
39 ?: =(c d)
40 $(a (rsh 3 1 a), b (rsh 3 1 b))
41 (lth c d)
42```
44#### Examples
46```
47 > (aor 'a' 'b')
48 %.y
50 > (aor 'b' 'a')
51 %.n
53 > (aor 'a' 'a')
54 %.y
56 > (aor "foo" "bar")
57 %.n
59 > (aor "bar" "foo")
60 %.y
62 > (aor "abcdefz" "abcdefa")
63 %.n
65 > (aor "abcdefa" "abcdefz")
66 %.y
68 > (aor 10.000 17.000)
69 %.y
71 > (aor 10 9)
72 %.n
73```
76---
77### `++dor`
79Numeric order
81Computes whether `a` and `b` are in ascending numeric order, producing a flag.
83#### Accepts
85`a` is a noun.
87`b` is a noun.
89#### Produces
91A flag.
93#### Source
95```hoon
96 ++ dor
97 ~/ %dor
98 |= [a=* b=*]
99 ^- ?
100 ?: =(a b) &
101 ?. ?=(@ a)
102 ?: ?=(@ b) |
103 ?: =(-.a -.b)
104 $(a +.a, b +.b)
105 $(a -.a, b -.b)
106 ?. ?=(@ b) &
107 (lth a b)
108```
110#### Examples
112```
113 > (dor 1 2)
114 %.y
116 > (dor 2 1)
117 %.n
119 > (dor ~[1 2 3] ~[1 2 4])
120 %.y
122 > (dor ~[1 2 4] ~[1 2 3])
123 %.n
125 > (dor `(list @)`~[99 100 10.000] ~[99 101 10.000])
126 %.y
128 > (dor ~[99 101 10.999] `(list @)`~[99 100 10.000])
129 %.n
130```
132#### Discussion
134If `a` and `b` are both atoms, `dor` is equivalent to [`lte`](@/docs/hoon/reference/stdlib/1a.md). If they're
135cells, `dor` recurses on the heads, and then if the heads are the same it checks
136the tails.
138If one sample is a cell and the other is an atom, the cell sample is treated as
139"greater."
142---
143### `++gor`
145Hash order
147Computes whether of `(mug a)` and `(mug b)` are in ascending numeric order,
148producing a flag. If the `mug` hashes are equal, `a` and `b` are compared by
149`dor` instead.
151[`mug`](@/docs/hoon/reference/stdlib/2e.md) is the the 31-bit nonzero FNV-1a hash algorithm.
153#### Accepts
155`a` is a noun.
157`b` is a noun.
159#### Produces
161A flag.
163#### Source
165```hoon
166 ++ gor
167 ~/ %gor
168 |= [a=* b=*]
169 ^- ?
170 =+ [c=(mug a) d=(mug b)]
171 ?: =(c d)
172 (dor a b)
173 (lth c d)
174```
176#### Examples
178```
179 > (gor 'd' 'c')
180 %.y
182 > 'd'
183 'd'
185 > 'c'
186 'c'
188 > `@ud`'d'
189 100
191 > `@ud`'c'
192 99
194 > (mug 'd')
195 1.628.185.714
197 > (mug 'c')
198 1.712.073.811
200 > (gor 'd' 'c')
201 %.y
203 > (gor 'c' 'd')
204 %.n
206 > (gor "foo" "bar")
207 %.n
209 > (gor (some 10) `(list @)`[1 2 3 ~])
210 %.n
211```
213#### Discussion
215`map`s use `gor` on the key for horizontal ordering and `vor` for
216vertical order. `map`s only look at the keys (the head of the key-value pair
217elements) for ordering.
219---
220### `++hor`
222Horizontal hash order
224If `a` and `b` are atoms, they are computed as samples of `gor`, producing a
225flag.
227If `a` and `b` are cells, `hor` checks to see if their heads are equal. If their
228heads are equal, `gor` is computed on their tails, producing a flag. If their
229heads are not equal, `gor` is computed on their heads, producing a flag.
231If `a` is an atom and `b` is not, then `%.y` is produced. If `b` is an atom and
232`a` is not, `%.n` is produced.
234#### Accepts
236`a` is a noun.
238`b` is a noun.
240#### Produces
242A flag.
244#### Source
246```hoon
247 ++ hor
248 ~/ %hor
249 |= [a=* b=*]
250 ^- ?
251 ?: ?=(@ a)
252 ?. ?=(@ b) &
253 (gor a b)
254 ?: ?=(@ b) |
255 ?: =(-.a -.b)
256 (gor +.a +.b)
257 (gor -.a -.b)
258```
260#### Examples
262```
263 > (hor . 1)
264 %.n
266 > (hor 1 2)
267 %.y
269 > (hor "abc" "cba")
270 %.y
272 > (hor 'c' 'd')
273 %.n
275 > (hor 100 [1 1])
276 %.y
278 > (hor [1 1] 100)
279 %.n
281 > (hor 500.000.000 "foo")
282 %.y
284 > (hor "foo" 500.000.000)
285 %.n
287 > (hor 0 "foo")
288 %.y
290 > (gor `(list @)`[0 ~] 1)
291 %.n
293 > (hor `(list @)`[50 500 5.000 ~] 1)
294 %.n
295```
297#### Discussion
299Sets (+set, +in) use `hor` for their horizontal order and `vor` for their
300vertical order. See [`apt:in`](@/docs/hoon/reference/stdlib/2h.md) to see how a set verifies its correctness.
301`vor` is used to verify.
303---
304### `++lor`
306Leg order
308Computes whether the corresponding legs of `a` and `b` are in ascending
309numeric order, producing a flag.
311#### Accepts
313`a` is a noun.
315`b` is a noun.
317#### Accepts
319A flag.
321#### Source
323```hoon
324 ++ lor
325 ~/ %lor
326 |= [a=* b=*]
327 ^- ?
328 ?: =(a b) &
329 ?@ a
330 ?^ b &
331 (lth a b)
332 ?: =(-.a -.b)
333 $(a +.a, b +.b)
334 $(a -.a, b -.b)
335```
337#### Examples
339```
340 > (lor 1 2)
341 %.y
343 > (lor 2 1)
344 %.n
346 > (lor 9 [1 2])
347 %.y
349 > (lor `(list @)`[1 2 3 ~] `(list @)`[1 2 3 4 ~])
350 %.y
352 > (lor `(list @)`[1 2 3 ~] `(list @)`[1 2 2 ~])
353 %.n
355 > (lor `(list @)`[1 2 3 ~] `(list @)`[2 2 ~])
356 %.y
358 > (lor [2 2 5] [1 5 9 8])
359 %.n
361 > (lor [1 5 8] [1 5 9 8])
362 %.y
364 > (lor [1 2 3] [1 2])
365 !! exit
366```
368#### Discussion
370`lor` is no longer used internally.
372---
373### `++vor`
375Vertical order
377Computes whether the double-hashes `(mug (mug a))` and `(mug (mug b))` are in
378ascending numeric order, producing a flag. If the double-`mug` hashes are
379equal, `a` and `b` are compared by `dor` instead.
381[`mug`](@/docs/hoon/reference/stdlib/2e.md) is the the 31-bit nonzero FNV-1a hash algorithm.
384#### Accepts
386`a` is a noun
388`b` is a noun
390#### Produces
392A flag.
394#### Source
396```hoon
397 ++ vor
398 ~/ %vor
399 |= [a=* b=*]
400 ^- ?
401 =+ [c=(mug (mug a)) d=(mug (mug b))]
402 ?: =(c d)
403 (dor a b)
404 (lth c d)
405```
407#### Examples
409```
410 > (vor 'f' 'g')
411 %.y
413 > [(mug 'f') (mug 'g')]
414 [1.661.740.952 1.644.963.335]
416 > [(mug (mug 'f')) (mug (mug 'g'))]
417 [261.421.509 1.861.258.547]
419 > (vor 'a' 'z')
420 %.n
422 > (vor 43.326 41.106)
423 %.n
424```
426#### Discussion
428Maps, sets, and queues all use `vor` to check for vertical ordering. Maps and
429sets also use `gor` and `hor` for horizontal order, respectively, but queues use
430vertical ordering alone.
432Since hashing removes correlation, double-`mug`ging with `vor` removes
433correlation with single-`mug`ged `hor`. Vertical order becomes uncorrelated
434with horizontal order.
437---