-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremainder.robin
65 lines (47 loc) · 1.57 KB
/
remainder.robin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
;'<<SPEC'
-> Tests for functionality "Evaluate Robin Expression (with Arith)"
`remainder` evaluates both of its arguments to numbers and evaluates to the
remainder of the division of the first number by the second.
| (remainder 12 3)
= 0
| (remainder 11 3)
= 2
| (remainder 10 3)
= 1
| (remainder 9 3)
= 0
The remainder is *always non-negative*.
| (remainder (subtract 0 10) 3)
= 2
| (remainder 10 (subtract 0 3))
= 2
Trying to find the remainder of a division by zero is undefined, and an
abort value will be produced.
| (remainder 10 0)
? abort (division-by-zero 10)
`remainder` expects exactly two arguments, both numbers.
| (remainder 14)
? abort (illegal-arguments
| (remainder 14 23 57)
? abort (illegal-arguments
| (remainder 14 #t)
? abort (expected-number #t)
| (remainder #t 51)
? abort (expected-number #t)
'<<SPEC'
(define remainder (fun (n d)
(bind remainder-r-pos (fun (self n d acc) ;(d is positive)
(if (gt? d n)
n
(self self (subtract n d) d (add 1 acc))))
(bind remainder-r-neg (fun (self n d acc) ;(d is negative)
(if (gt? (abs d) n)
(add 1 n)
(self self (add n d) d (add 1 acc))))
(if (equal? d 0)
(abort (list (literal division-by-zero) n))
(bind n-prime (if (lt? n 0) (subtract 0 n) n)
(bind d-prime (if (lt? n 0) (subtract 0 d) d)
(if (gt? d-prime 0)
(remainder-r-pos remainder-r-pos n-prime d-prime 0)
(remainder-r-neg remainder-r-neg n-prime d-prime 0)))))))))