on b93c19b help sign in
679 lines 6.6 kB history raw
1+++
2title = "1a: Basic Arithmetic"
3weight = 1
4template = "doc.html"
5+++
6### `++add`
8Add
10Produces the sum of `a` and `b`.
12#### Accepts
14`a` is an atom.
16`b` is an atom.
18#### Produces
20An atom.
22#### Source
24```
25 ++ add
26 ~/ %add
27 |= [a=@ b=@]
28 ^- @
29 ?: =(0 a) b
30 $(a (dec a), b +(b))
31```
33#### Examples
35```
36 > (add 2 2)
37 4
39 > (add 1 1.000.000)
40 1.000.001
42 > (add 1.333 (mul 2 2))
43 1.337
45 > (add 'a' 4)
46 101
48 > (add 'a' 'b')
49 195
51 > (add 0xc 0xf)
52 27
54 > `@ux`(add 0xc 0xf)
55 0x1b
56```
58---
59### `++dec`
61Decrement
63Decrements `a` by `1`.
65#### Accepts
67`a` is an atom.
69#### Produces
71An atom.
73#### Source
75```
76 ++ dec
77 ~/ %dec
78 |= a=@
79 ~| %decrement-underflow
80 ?< =(0 a)
81 =+ b=0
82 |- ^- @
83 ?: =(a +(b)) b
84 $(b +(b))
85```
87#### Examples
89```
90 > (dec 7)
91 6
93 > (dec 0)
94 ! decrement-underflow
95 ! exit
97 > (dec 'b')
98 97
99 > `@t`(dec 'b')
100 'a'
102 > (dec 0xc)
103 11
104 > `@ux`(dec 0xc)
105 0xb
106```
109---
110### `++div`
112Divide
114Computes `a` divided by `b` without remainder.
116#### Accepts
118`a` is an atom.
120`b` is an atom.
122#### Produces
124An atom.
126#### Source
128```
129 ++ div
130 ~/ %div
131 |: [a=`@`1 b=`@`1]
132 ^- @
133 ~_ leaf+"divide-by-zero"
134 ?< =(0 b)
135 =+ c=0
136 |-
137 ?: (lth a b) c
138 $(a (sub a b), c +(c))
139```
141#### Examples
143```
144 > (div 4 2)
145 2
147 > (div 17 8)
148 2
150 > (div 20 30)
151 0
153 > (div 'ab' 'a')
154 259
155 > `@`'ab'
156 25.185
157 > `@`a
158 97
160 > (div 0x9f 0x29)
161 6
162```
164---
165### `++dvr`
167Divide with remainder
169Computes `a` divided by `b`, producing the quotient and the remainder.
171#### Accepts
173`a` is an atom.
175`b` is an atom.
177#### Produces
179A cell of atoms.
181#### Source
183```
184 ++ dvr
185 ~/ %dvr
186 |= [a=@ b=@]
187 ^- [p=@ q=@]
188 [(div a b) (mod a b)]
189```
191#### Examples
193```
194 > (dvr 17 8)
195 [p=2 q=1]
197 > (dvr 17 6)
198 [p=2 q=5]
200 > (dvr 18 6)
201 [p=3 q=0]
203 > (dvr 4 2)
204 [p=2 q=0]
206 > (dvr 'ab 'a')
207 [p=259 q=62]
208 > `@`'a'
209 97
210 > `@`'ab'
211 25.185
212```
214---
215### `++gte`
217Greater-than/equal
219Tests whether `a` is greater than or equal to `b`.
221#### Accepts
223`a` is an atom.
225`b` is an atom.
227#### Produces
229A flag.
231#### Source
233```
234 ++ gte
235 ~/ %gte
236 |= [a=@ b=@]
237 ^- ?
238 !(lth a b)
239```
241#### Examples
243```
244 > (gte 100 10)
245 %.y
247 > (gte 4 4)
248 %.y
250 > (gte 3 4)
251 %.n
252```
254---
255### `++gth`
257Greater-than
259Tests whether `a` is greater than `b`.
261#### Accepts
263`a` is an atom.
265`b` is an atom.
267#### Produces
269A flag.
271#### Source
273```
274 ++ gth
275 ~/ %gth
276 |= [a=@ b=@]
277 ^- ?
278 !(lte a b)
279```
282#### Examples
284```
285 > (gth 'd' 'c')
286 %.y
288 > (gth ~h1 ~m61)
289 %.n
291 > (gth 'steak' 'brisket')
292 %.n
293```
295---
296### `++lte`
298Less-than/equal
300Tests whether `a` is less than or equal to `b`.
302#### Accepts
304`a` is an atom.
306`b` is an atom.
308#### Produces
310A flag.
312#### Source
314```
315 ++ lte
316 ~/ %lte
317 |= [a=@ b=@]
318 |(=(a b) (lth a b))
319```
321#### Examples
323```
324 > (lte 4 5)
325 %.y
327 > (lte 5 4)
328 %.n
330 > (lte 5 5)
331 %.y
333 > (lte 0 0)
334 %.y
337 > (lte 'a' 'b')
338 %.y
339 > `@`'a'
340 97
341 > `@`'b'
342 98
344 > (lte 'abraham' 'bob')
345 %.n
347 > (lte ~m61 ~h1)
348 %.n
349```
352---
353### `++lth`
355Less-than
357Tests whether `a` is less than `b`.
359#### Accepts
361`a` is an atom.
363`b` is an atom.
365#### Produces
367A flag.
369#### Source
371```
372 ++ lth
373 ~/ %lth
374 |= [a=@ b=@]
375 ^- ?
376 ?& !=(a b)
377 |-
378 ?| =(0 a)
379 ?& !=(0 b)
380 $(a (dec a), b (dec b))
381 == == ==
382```
384#### Examples
386```
387 > (lth 4 5)
388 %.y
390 > (lth 5 4)
391 %.n
393 > (lth 5 5)
394 %.n
396 > (lth 5 0)
397 %.n
399 > (lth 'a' 'b')
400 %.y
401 > `@`'a'
402 97
403 > `@`'b'
404 98
406 > (lth 'abraham' 'bob')
407 %.n
408```
410---
411### `++max`
413Maximum
415Computes the greater of `a` and `b`.
417#### Accepts
419`a` is an atom.
421`b` is an atom.
423#### Produces
425An atom.
427#### Source
429```
430 ++ max
431 ~/ %max
432 |= [a=@ b=@]
433 ^- @
434 ?: (gth a b) a
435 b
436```
438#### Examples
440```
441 > (max 10 100)
442 100
444 > (max 10.443 9)
445 10.443
447 > (max 1 1)
448 1
450 > (max 'abraham' 'bob')
451 30.787.873.400.840.801
453 > `@t`(max 'abraham' 'bob')
454 'abraham'
456 > `@t`(max 'abraham' 'benjamin')
457 'benjamin'
459 > `@dr`(max ~m6 ~h1)
460 ~h1
462 >`@ux`(max 0xe1 0x1e)
463 0xe1
464```
466---
467### `++min`
469Minimum
471Computes the lesser of `a` and `b`.
473#### Accepts
475`a` is an atom.
477`b` is an atom.
479#### Produces
481An atom.
483#### Source
485```
486 ++ min
487 ~/ %min
488 |= [a=@ b=@]
489 ^- @
490 ?: (lth a b) a
491 b
492```
494#### Examples
496```
497 > (min 10 100)
498 10
500 > (min 10.443 9)
501 9
503 > (min 1 1)
504 1
506 > (min 'abraham' 'bob')
507 6.451.042
509 > `@t`(min 'abraham' 'bob')
510 'bob'
512 > `@t`(min 'abraham' 'benjamin')
513 'abraham'
515 > `@dr`(min ~m6 ~h1)
516 ~m6
517```
520---
521### `++mod`
523Modulus
525Computes the remainder of dividing `a` by `b`.
527#### Accepts
529`a` is an atom.
531`b` is an atom.
533#### Produces
535An atom.
537#### Source
539```
540 ++ mod
541 ~/ %mod
542 |: [a=`@`1 b=`@`1]
543 ^- @
544 ?< =(0 b)
545 (sub a (mul b (div a b)))
546```
548#### Examples
550```
551 > (mod 5 2)
552 1
554 > (mod 5 5)
555 0
557 > (mod 2 5)
558 2
560 > (mod 40 20)
561 0
563 > `@`'c'
564 99
565 >`@`'a'
566 97
567 > (mod 'c' 'a')
568 2
569```
571---
572### `++mul`
574Multiply
576Multiplies `a` by `b`.
578#### Accepts
580`a` is an atom.
582`b` is an atom.
584#### Produces
586An atom.
588#### Source
590```
591 ++ mul
592 ~/ %mul
593 |: [a=`@`1 b=`@`1]
594 ^- @
595 =+ c=0
596 |-
597 ?: =(0 a) c
598 $(a (dec a), c (add b c))
599```
601#### Examples
603```
604 > (mul 3 4)
605 12
607 > (mul 0 1)
608 0
610 > `@`'a'
611 97
612 > `@`'b'
613 98
614 > (mul 'a' 'b')
615 9.506
617 >`@ux`(mul 0x1f 0xe)
618 0x1b2
619```
622---
623### `++sub`
625Subtract
627Subtracts `b` from `a`.
629#### Accepts
631`a` is an atom.
633`b` is an atom.
635#### Produces
637An atom.
639#### Source
641```
642 ++ sub
643 ~/ %sub
644 |= [a=@ b=@]
645 ~_ leaf+"subtract-underflow"
646 ^- @
647 ?: =(0 b) a
648 $(a (dec a), b (dec b))
649```
652#### Examples
654```
655 > (sub 10 5)
656 5
658 > (sub 243 44)
659 199
661 > (sub 5 0)
662 5
664 > (sub 0 5)
665 ! subtract-underflow
666 ! exit
668 > (sub 'c' 'a')
669 2
670 > `@`'c'
671 99
672 > `@`'a'
673 97
675 > (sub 0x1f 0xe)
676 17
677 > `@ux`(sub 0x1f 0xe)
678 0x11
679```