on c19bc7a help sign in
684 lines 6.7 kB history raw
1:- :~ navhome/'/docs/'
2 sort/'1'
3 title/'1a: Basic Arithmetic'
4 ==
6;>
9# 1a: basic arithmetic
11### `++add`
13Add
15Produces the sum of `a` and `b`.
17#### Accepts
19`a` is an atom.
21`b` is an atom.
23#### Produces
25An atom.
27#### Source
29```
30 ++ add
31 ~/ %add
32 |= [a=@ b=@]
33 ^- @
34 ?: =(0 a) b
35 $(a (dec a), b +(b))
36```
38#### Examples
40```
41 > (add 2 2)
42 4
44 > (add 1 1.000.000)
45 1.000.001
47 > (add 1.333 (mul 2 2))
48 1.337
50 > (add 'a' 4)
51 101
53 > (add 'a' 'b')
54 195
56 > (add 0xc 0xf)
57 27
59 > `@ux`(add 0xc 0xf)
60 0x1b
61```
63---
64### `++dec`
66Decrement
68Decrements `a` by `1`.
70#### Accepts
72`a` is an atom.
74#### Produces
76An atom.
78#### Source
80```
81 ++ dec
82 ~/ %dec
83 |= a=@
84 ~| %decrement-underflow
85 ?< =(0 a)
86 =+ b=0
87 |- ^- @
88 ?: =(a +(b)) b
89 $(b +(b))
90```
92#### Examples
94```
95 > (dec 7)
96 6
98 > (dec 0)
99 ! decrement-underflow
100 ! exit
102 > (dec 'b')
103 97
104 > `@t`(dec 'b')
105 'a'
107 > (dec 0xc)
108 11
109 > `@ux`(dec 0xc)
110 0xb
111```
114---
115### `++div`
117Divide
119Computes `a` divided by `b` without remainder.
121#### Accepts
123`a` is an atom.
125`b` is an atom.
127#### Produces
129An atom.
131#### Source
133```
134 ++ div
135 ~/ %div
136 |: [a=`@`1 b=`@`1]
137 ^- @
138 ~_ leaf+"divide-by-zero"
139 ?< =(0 b)
140 =+ c=0
141 |-
142 ?: (lth a b) c
143 $(a (sub a b), c +(c))
144```
146#### Examples
148```
149 > (div 4 2)
150 2
152 > (div 17 8)
153 2
155 > (div 20 30)
156 0
158 > (div 'ab' 'a')
159 259
160 > `@`'ab'
161 25.185
162 > `@`a
163 97
165 > (div 0x9f 0x29)
166 6
167```
169---
170### `++dvr`
172Divide with remainder
174Computes `a` divided by `b`, producing the quotient and the remainder.
176#### Accepts
178`a` is an atom.
180`b` is an atom.
182#### Produces
184A cell of atoms.
186#### Source
188```
189 ++ dvr
190 ~/ %dvr
191 |= [a=@ b=@]
192 ^- [p=@ q=@]
193 [(div a b) (mod a b)]
194```
196#### Examples
198```
199 > (dvr 17 8)
200 [p=2 q=1]
202 > (dvr 17 6)
203 [p=2 q=5]
205 > (dvr 18 6)
206 [p=3 q=0]
208 > (dvr 4 2)
209 [p=2 q=0]
211 > (dvr 'ab 'a')
212 [p=259 q=62]
213 > `@`'a'
214 97
215 > `@`'ab'
216 25.185
217```
219---
220### `++gte`
222Greater-than/equal
224Tests whether `a` is greater than or equal to `b`.
226#### Accepts
228`a` is an atom.
230`b` is an atom.
232#### Produces
234A flag.
236#### Source
238```
239 ++ gte
240 ~/ %gte
241 |= [a=@ b=@]
242 ^- ?
243 !(lth a b)
244```
246#### Examples
248```
249 > (gte 100 10)
250 %.y
252 > (gte 4 4)
253 %.y
255 > (gte 3 4)
256 %.n
257```
259---
260### `++gth`
262Greater-than
264Tests whether `a` is greater than `b`.
266#### Accepts
268`a` is an atom.
270`b` is an atom.
272#### Produces
274A flag.
276#### Source
278```
279 ++ gth
280 ~/ %gth
281 |= [a=@ b=@]
282 ^- ?
283 !(lte a b)
284```
287#### Examples
289```
290 > (gth 'd' 'c')
291 %.y
293 > (gth ~h1 ~m61)
294 %.n
296 > (gth 'steak' 'brisket')
297 %.n
298```
300---
301### `++lte`
303Less-than/equal
305Tests whether `a` is less than or equal to `b`.
307#### Accepts
309`a` is an atom.
311`b` is an atom.
313#### Produces
315A flag.
317#### Source
319```
320 ++ lte
321 ~/ %lte
322 |= [a=@ b=@]
323 |(=(a b) (lth a b))
324```
326#### Examples
328```
329 > (lte 4 5)
330 %.y
332 > (lte 5 4)
333 %.n
335 > (lte 5 5)
336 %.y
338 > (lte 0 0)
339 %.y
342 > (lte 'a' 'b')
343 %.y
344 > `@`'a'
345 97
346 > `@`'b'
347 98
349 > (lte 'abraham' 'bob')
350 %.n
352 > (lte ~m61 ~h1)
353 %.n
354```
357---
358### `++lth`
360Less-than
362Tests whether `a` is less than `b`.
364#### Accepts
366`a` is an atom.
368`b` is an atom.
370#### Produces
372A flag.
374#### Source
376```
377 ++ lth
378 ~/ %lth
379 |= [a=@ b=@]
380 ^- ?
381 ?& !=(a b)
382 |-
383 ?| =(0 a)
384 ?& !=(0 b)
385 $(a (dec a), b (dec b))
386 == == ==
387```
389#### Examples
391```
392 > (lth 4 5)
393 %.y
395 > (lth 5 4)
396 %.n
398 > (lth 5 5)
399 %.n
401 > (lth 5 0)
402 %.n
404 > (lth 'a' 'b')
405 %.y
406 > `@`'a'
407 97
408 > `@`'b'
409 98
411 > (lth 'abraham' 'bob')
412 %.n
413```
415---
416### `++max`
418Maximum
420Computes the greater of `a` and `b`.
422#### Accepts
424`a` is an atom.
426`b` is an atom.
428#### Produces
430An atom.
432#### Source
434```
435 ++ max
436 ~/ %max
437 |= [a=@ b=@]
438 ^- @
439 ?: (gth a b) a
440 b
441```
443#### Examples
445```
446 > (max 10 100)
447 100
449 > (max 10.443 9)
450 10.443
452 > (max 1 1)
453 1
455 > (max 'abraham' 'bob')
456 30.787.873.400.840.801
458 > `@t`(max 'abraham' 'bob')
459 'abraham'
461 > `@t`(max 'abraham' 'benjamin')
462 'benjamin'
464 > `@dr`(max ~m6 ~h1)
465 ~h1
467 >`@ux`(max 0xe1 0x1e)
468 0xe1
469```
471---
472### `++min`
474Minimum
476Computes the lesser of `a` and `b`.
478#### Accepts
480`a` is an atom.
482`b` is an atom.
484#### Produces
486An atom.
488#### Source
490```
491 ++ min
492 ~/ %min
493 |= [a=@ b=@]
494 ^- @
495 ?: (lth a b) a
496 b
497```
499#### Examples
501```
502 > (min 10 100)
503 10
505 > (min 10.443 9)
506 9
508 > (min 1 1)
509 1
511 > (min 'abraham' 'bob')
512 6.451.042
514 > `@t`(min 'abraham' 'bob')
515 'bob'
517 > `@t`(min 'abraham' 'benjamin')
518 'abraham'
520 > `@dr`(min ~m6 ~h1)
521 ~m6
522```
525---
526### `++mod`
528Modulus
530Computes the remainder of dividing `a` by `b`.
532#### Accepts
534`a` is an atom.
536`b` is an atom.
538#### Produces
540An atom.
542#### Source
544```
545 ++ mod
546 ~/ %mod
547 |: [a=`@`1 b=`@`1]
548 ^- @
549 ?< =(0 b)
550 (sub a (mul b (div a b)))
551```
553#### Examples
555```
556 > (mod 5 2)
557 1
559 > (mod 5 5)
560 0
562 > (mod 2 5)
563 2
565 > (mod 40 20)
566 0
568 > `@`'c'
569 99
570 >`@`'a'
571 97
572 > (mod 'c' 'a')
573 2
574```
576---
577### `++mul`
579Multiply
581Multiplies `a` by `b`.
583#### Accepts
585`a` is an atom.
587`b` is an atom.
589#### Produces
591An atom.
593#### Source
595```
596 ++ mul
597 ~/ %mul
598 |: [a=`@`1 b=`@`1]
599 ^- @
600 =+ c=0
601 |-
602 ?: =(0 a) c
603 $(a (dec a), c (add b c))
604```
606#### Examples
608```
609 > (mul 3 4)
610 12
612 > (mul 0 1)
613 0
615 > `@`'a'
616 97
617 > `@`'b'
618 98
619 > (mul 'a' 'b')
620 9.506
622 >`@ux`(mul 0x1f 0xe)
623 0x1b2
624```
627---
628### `++sub`
630Subtract
632Subtracts `b` from `a`.
634#### Accepts
636`a` is an atom.
638`b` is an atom.
640#### Produces
642An atom.
644#### Source
646```
647 ++ sub
648 ~/ %sub
649 |= [a=@ b=@]
650 ~_ leaf+"subtract-underflow"
651 ^- @
652 ?: =(0 b) a
653 $(a (dec a), b (dec b))
654```
657#### Examples
659```
660 > (sub 10 5)
661 5
663 > (sub 243 44)
664 199
666 > (sub 5 0)
667 5
669 > (sub 0 5)
670 ! subtract-underflow
671 ! exit
673 > (sub 'c' 'a')
674 2
675 > `@`'c'
676 99
677 > `@`'a'
678 97
680 > (sub 0x1f 0xe)
681 17
682 > `@ux`(sub 0x1f 0xe)
683 0x11
684```