on 6d45ede help sign in
509 lines 5.4 kB history raw
1---
2navhome: /docs
3---
6# 1a: basic arithmetic
7### `++add`
9Add
11Produces the sum of `a` and `b`.
13Accepts
14-------
16`a` is an atom.
18`b` is an atom.
20Produces
21--------
23An atom.
25Source
26------
28 ++ add :: add
29 ~/ %add
30 |= [a/@ b/@]
31 ^- @
32 ?: =(0 a) b
33 $(a (dec a), b +(b))
35Examples
36--------
38 > (add 2 2)
39 4
40 > (add 1 1.000.000)
41 1.000.001
42 > (add 1.333 (mul 2 2))
43 1.337
45***
46### `++dec`
48Decrement
50Decrements `a` by `1`.
52Accepts
53-------
55`a` is an atom.
57Produces
58--------
60An atom.
62Source
63------
65 ++ dec :: decrement
66 ~/ %dec
67 |= a/@
68 ~| %decrement-underflow
69 ?< =(0 a)
70 =+ b=0
71 |- ^- @
72 ?: =(a +(b)) b
73 $(b +(b))
75Examples
76--------
78 > (dec 7)
79 6
80 > (dec 0)
81 ! decrement-underflow
82 ! exit
86***
87### `++div`
89Divide
91Computes `a` divided by `b`.
93Accepts
94-------
96`a` is an atom.
98`b` is an atom.
100Produces
101--------
103An atom.
105Source
106------
108 ++ div :: divide
109 ~/ %div
110 |= [a/@ b/@]
111 ^- @
112 ~| 'div'
113 ?< =(0 b)
114 =+ c=0
115 |-
116 ?: (lth a b) c
117 $(a (sub a b), c +(c))
118 ::
120Examples
121--------
123 > (div 4 2)
124 2
125 > (div 17 8)
126 2
127 > (div 20 30)
128 0
131***
132### `++gte`
134Greater-than/equal
136Tests whether `a` is greater than a number `b`.
138Accepts
139-------
141`a` is an atom.
143`b` is an atom.
145Produces
146--------
148A boolean.
150Source
151------
153 ++ gte :: greater-equal
154 ~/ %gte
155 |= {a/@ b/@}
156 ^- ?
157 !(lth a b)
160Examples
161--------
163 > (gte 100 10)
164 %.y
165 > (gte 4 4)
166 %.y
167 > (gte 3 4)
168 %.n
172***
173### `++gth`
175Greater-than
177Tests whether `a` is greater than `b`.
179Accepts
180-------
182`a` is an atom.
184`b` is an atom.
186Produces
187--------
189A boolean.
191Source
192------
194 ++ gth :: greater-than
195 ~/ %gth
196 |= {a/@ b/@}
197 ^- ?
198 !(lte a b)
202Examples
203--------
205 > (gth 'd' 'c')
206 %.y
207 > (gth ~h1 ~m61)
208 %.n
212***
213### `++lte`
215Less-than/equal
217Tests whether `a` is less than or equal to `b`.
219Accepts
220-------
222`a` is an atom.
224`b` is an atom.
226Produces
227--------
229A boolean.
231Source
232------
234 ++ lte :: less-equal
235 ~/ %lte
236 |= {a/@ b/@}
237 |(=(a b) (lth a b))
239 ::
241Examples
242--------
244 > (lte 4 5)
245 %.y
246 > (lte 5 4)
247 %.n
248 > (lte 5 5)
249 %.y
250 > (lte 0 0)
251 %.y
255***
256### `++lth`
258Less-than
260Tests whether `a` is less than `b`.
262Accepts
263-------
265`a` is an atom.
267`b` is an atom.
269Produces
270--------
272A boolean.
274Source
275------
277 ++ lth :: less-than
278 ~/ %lth
279 |= {a/@ b/@}
280 ^- ?
281 ?& !=(a b)
282 |-
283 ?| =(0 a)
284 ?& !=(0 b)
285 $(a (dec a), b (dec b))
286 == == ==
288Examples
289--------
291 > (lth 4 5)
292 %.y
293 > (lth 5 4)
294 %.n
295 > (lth 5 5)
296 %.n
297 > (lth 5 0)
298 %.n
301***
302### `++max`
304Maximum
306Computes the greater of `a` and `b`.
308Accepts
309-------
311`a` is an atom.
313`b` is an atom.
315Produces
316--------
318An atom.
320Source
321------
323 ++ max :: maximum
324 ~/ %max
325 |= {a/@ b/@}
326 ^- @
327 ?: (gth a b) a
328 b
330Examples
331--------
333 > (max 10 100)
334 100
335 > (max 10.443 9)
336 10.443
337 > (max 0 1)
338 1
342***
343### `++min`
345Minimum
347Computes the lesser of `a` and `b`.
349Accepts
350-------
352`a` is an atom.
354`b` is an atom.
356Produces
357--------
359An atom.
361Source
362------
364 ++ min :: minimum
365 ~/ %min
366 |= {a/@ b/@}
367 ^- @
368 ?: (lth a b) a
369 b
371Examples
372--------
374 > (min 10 100)
375 10
376 > (min 10.443 9)
377 9
378 > (min 0 1)
379 0
383***
384### `++mod`
386Modulus
388Computes the remainder of dividing `a` by `b`.
390Accepts
391-------
393`a` is an atom.
395`b` is an atom.
397Produces
398--------
400An atom.
402Source
403------
405 ++ mod :: remainder
406 ~/ %mod
407 |: [a=`@`1 b=`@`1]
408 ^- @
409 ?< =(0 b)
410 (sub a (mul b (div a b)))
413Examples
414--------
416 > (mod 5 2)
417 1
418 > (mod 5 5)
419 0
423***
424### `++mul`
426Multiply
428Multiplies `a` by `b`.
430Accepts
431-------
433`a` is an atom.
435`b` is an atom.
437Produces
438--------
440An atom.
442Source
443------
445 ++ mul :: multiply
446 ~/ %mul
447 |: [a=`@`1 b=`@`1]
448 ^- @
449 =+ c=0
450 |-
451 ?: =(0 a) c
452 $(a (dec a), c (add b c))
454Examples
455--------
457 > (mul 3 4)
458 12
459 > (mul 0 1)
460 0
464***
465### `++sub`
467Subtract
469Subtracts `b` from `a`.
471Accepts
472-------
474`a` is an atom.
476`b` is an atom.
478Produces
479--------
481An atom.
483Source
484------
486 ++ sub :: subtract
487 ~/ %sub
488 |= [a/@ b/@]
489 ~| %subtract-underflow
490 ^- @
491 ?: =(0 b) a
492 $(a (dec a), b (dec b))
494Examples
495--------
497 > (sub 10 5)
498 5
499 > (sub 243 44)
500 199
501 > (sub 5 0)
502 5
503 > (sub 0 5)
504 ! subtract-underflow
505 ! exit
509***