on 979ae21 help sign in
680 lines 6.7 kB history raw
1+++
2title = "1a: Basic Arithmetic"
3weight = 1
4template = "doc.html"
5+++
7### `++add`
9Add
11Produces the sum of `a` and `b`.
13#### Accepts
15`a` is an atom.
17`b` is an atom.
19#### Produces
21An atom.
23#### Source
25```hoon
26 ++ add
27 ~/ %add
28 |= [a=@ b=@]
29 ^- @
30 ?: =(0 a) b
31 $(a (dec a), b +(b))
32```
34#### Examples
36```
37 > (add 2 2)
38 4
40 > (add 1 1.000.000)
41 1.000.001
43 > (add 1.333 (mul 2 2))
44 1.337
46 > (add 'a' 4)
47 101
49 > (add 'a' 'b')
50 195
52 > (add 0xc 0xf)
53 27
55 > `@ux`(add 0xc 0xf)
56 0x1b
57```
59---
60### `++dec`
62Decrement
64Decrements `a` by `1`.
66#### Accepts
68`a` is an atom.
70#### Produces
72An atom.
74#### Source
76```hoon
77 ++ dec
78 ~/ %dec
79 |= a=@
80 ~| %decrement-underflow
81 ?< =(0 a)
82 =+ b=0
83 |- ^- @
84 ?: =(a +(b)) b
85 $(b +(b))
86```
88#### Examples
90```
91 > (dec 7)
92 6
94 > (dec 0)
95 ! decrement-underflow
96 ! exit
98 > (dec 'b')
99 97
100 > `@t`(dec 'b')
101 'a'
103 > (dec 0xc)
104 11
105 > `@ux`(dec 0xc)
106 0xb
107```
110---
111### `++div`
113Divide
115Computes `a` divided by `b` without remainder.
117#### Accepts
119`a` is an atom.
121`b` is an atom.
123#### Produces
125An atom.
127#### Source
129```hoon
130 ++ div
131 ~/ %div
132 |: [a=`@`1 b=`@`1]
133 ^- @
134 ~_ leaf+"divide-by-zero"
135 ?< =(0 b)
136 =+ c=0
137 |-
138 ?: (lth a b) c
139 $(a (sub a b), c +(c))
140```
142#### Examples
144```
145 > (div 4 2)
146 2
148 > (div 17 8)
149 2
151 > (div 20 30)
152 0
154 > (div 'ab' 'a')
155 259
156 > `@`'ab'
157 25.185
158 > `@`a
159 97
161 > (div 0x9f 0x29)
162 6
163```
165---
166### `++dvr`
168Divide with remainder
170Computes `a` divided by `b`, producing the quotient and the remainder.
172#### Accepts
174`a` is an atom.
176`b` is an atom.
178#### Produces
180A cell of atoms.
182#### Source
184```hoon
185 ++ dvr
186 ~/ %dvr
187 |= [a=@ b=@]
188 ^- [p=@ q=@]
189 [(div a b) (mod a b)]
190```
192#### Examples
194```
195 > (dvr 17 8)
196 [p=2 q=1]
198 > (dvr 17 6)
199 [p=2 q=5]
201 > (dvr 18 6)
202 [p=3 q=0]
204 > (dvr 4 2)
205 [p=2 q=0]
207 > (dvr 'ab 'a')
208 [p=259 q=62]
209 > `@`'a'
210 97
211 > `@`'ab'
212 25.185
213```
215---
216### `++gte`
218Greater-than/equal
220Tests whether `a` is greater than or equal to `b`.
222#### Accepts
224`a` is an atom.
226`b` is an atom.
228#### Produces
230A flag.
232#### Source
234```hoon
235 ++ gte
236 ~/ %gte
237 |= [a=@ b=@]
238 ^- ?
239 !(lth a b)
240```
242#### Examples
244```
245 > (gte 100 10)
246 %.y
248 > (gte 4 4)
249 %.y
251 > (gte 3 4)
252 %.n
253```
255---
256### `++gth`
258Greater-than
260Tests whether `a` is greater than `b`.
262#### Accepts
264`a` is an atom.
266`b` is an atom.
268#### Produces
270A flag.
272#### Source
274```hoon
275 ++ gth
276 ~/ %gth
277 |= [a=@ b=@]
278 ^- ?
279 !(lte a b)
280```
283#### Examples
285```
286 > (gth 'd' 'c')
287 %.y
289 > (gth ~h1 ~m61)
290 %.n
292 > (gth 'steak' 'brisket')
293 %.n
294```
296---
297### `++lte`
299Less-than/equal
301Tests whether `a` is less than or equal to `b`.
303#### Accepts
305`a` is an atom.
307`b` is an atom.
309#### Produces
311A flag.
313#### Source
315```hoon
316 ++ lte
317 ~/ %lte
318 |= [a=@ b=@]
319 |(=(a b) (lth a b))
320```
322#### Examples
324```
325 > (lte 4 5)
326 %.y
328 > (lte 5 4)
329 %.n
331 > (lte 5 5)
332 %.y
334 > (lte 0 0)
335 %.y
338 > (lte 'a' 'b')
339 %.y
340 > `@`'a'
341 97
342 > `@`'b'
343 98
345 > (lte 'abraham' 'bob')
346 %.n
348 > (lte ~m61 ~h1)
349 %.n
350```
353---
354### `++lth`
356Less-than
358Tests whether `a` is less than `b`.
360#### Accepts
362`a` is an atom.
364`b` is an atom.
366#### Produces
368A flag.
370#### Source
372```hoon
373 ++ lth
374 ~/ %lth
375 |= [a=@ b=@]
376 ^- ?
377 ?& !=(a b)
378 |-
379 ?| =(0 a)
380 ?& !=(0 b)
381 $(a (dec a), b (dec b))
382 == == ==
383```
385#### Examples
387```
388 > (lth 4 5)
389 %.y
391 > (lth 5 4)
392 %.n
394 > (lth 5 5)
395 %.n
397 > (lth 5 0)
398 %.n
400 > (lth 'a' 'b')
401 %.y
402 > `@`'a'
403 97
404 > `@`'b'
405 98
407 > (lth 'abraham' 'bob')
408 %.n
409```
411---
412### `++max`
414Maximum
416Computes the greater of `a` and `b`.
418#### Accepts
420`a` is an atom.
422`b` is an atom.
424#### Produces
426An atom.
428#### Source
430```hoon
431 ++ max
432 ~/ %max
433 |= [a=@ b=@]
434 ^- @
435 ?: (gth a b) a
436 b
437```
439#### Examples
441```
442 > (max 10 100)
443 100
445 > (max 10.443 9)
446 10.443
448 > (max 1 1)
449 1
451 > (max 'abraham' 'bob')
452 30.787.873.400.840.801
454 > `@t`(max 'abraham' 'bob')
455 'abraham'
457 > `@t`(max 'abraham' 'benjamin')
458 'benjamin'
460 > `@dr`(max ~m6 ~h1)
461 ~h1
463 >`@ux`(max 0xe1 0x1e)
464 0xe1
465```
467---
468### `++min`
470Minimum
472Computes the lesser of `a` and `b`.
474#### Accepts
476`a` is an atom.
478`b` is an atom.
480#### Produces
482An atom.
484#### Source
486```hoon
487 ++ min
488 ~/ %min
489 |= [a=@ b=@]
490 ^- @
491 ?: (lth a b) a
492 b
493```
495#### Examples
497```
498 > (min 10 100)
499 10
501 > (min 10.443 9)
502 9
504 > (min 1 1)
505 1
507 > (min 'abraham' 'bob')
508 6.451.042
510 > `@t`(min 'abraham' 'bob')
511 'bob'
513 > `@t`(min 'abraham' 'benjamin')
514 'abraham'
516 > `@dr`(min ~m6 ~h1)
517 ~m6
518```
521---
522### `++mod`
524Modulus
526Computes the remainder of dividing `a` by `b`.
528#### Accepts
530`a` is an atom.
532`b` is an atom.
534#### Produces
536An atom.
538#### Source
540```hoon
541 ++ mod
542 ~/ %mod
543 |: [a=`@`1 b=`@`1]
544 ^- @
545 ?< =(0 b)
546 (sub a (mul b (div a b)))
547```
549#### Examples
551```
552 > (mod 5 2)
553 1
555 > (mod 5 5)
556 0
558 > (mod 2 5)
559 2
561 > (mod 40 20)
562 0
564 > `@`'c'
565 99
566 >`@`'a'
567 97
568 > (mod 'c' 'a')
569 2
570```
572---
573### `++mul`
575Multiply
577Multiplies `a` by `b`.
579#### Accepts
581`a` is an atom.
583`b` is an atom.
585#### Produces
587An atom.
589#### Source
591```hoon
592 ++ mul
593 ~/ %mul
594 |: [a=`@`1 b=`@`1]
595 ^- @
596 =+ c=0
597 |-
598 ?: =(0 a) c
599 $(a (dec a), c (add b c))
600```
602#### Examples
604```
605 > (mul 3 4)
606 12
608 > (mul 0 1)
609 0
611 > `@`'a'
612 97
613 > `@`'b'
614 98
615 > (mul 'a' 'b')
616 9.506
618 >`@ux`(mul 0x1f 0xe)
619 0x1b2
620```
623---
624### `++sub`
626Subtract
628Subtracts `b` from `a`.
630#### Accepts
632`a` is an atom.
634`b` is an atom.
636#### Produces
638An atom.
640#### Source
642```hoon
643 ++ sub
644 ~/ %sub
645 |= [a=@ b=@]
646 ~_ leaf+"subtract-underflow"
647 ^- @
648 ?: =(0 b) a
649 $(a (dec a), b (dec b))
650```
653#### Examples
655```
656 > (sub 10 5)
657 5
659 > (sub 243 44)
660 199
662 > (sub 5 0)
663 5
665 > (sub 0 5)
666 ! subtract-underflow
667 ! exit
669 > (sub 'c' 'a')
670 2
671 > `@`'c'
672 99
673 > `@`'a'
674 97
676 > (sub 0x1f 0xe)
677 17
678 > `@ux`(sub 0x1f 0xe)
679 0x11
680```