commit b00065b8511ec5507c85584bdeddc5a02e1bb059
parent 602a59d9deda0b87d23dac8403305bc861227249
Author: Ryan Sepassi <rsepassi@gmail.com>
Date: Sat, 25 Apr 2026 14:33:10 -0700
scheme1: add min, max, modulo to the prelude
min/max are 2-arg derivations from <. modulo is the floor-flavor
companion to the truncating remainder primitive: same value when r
and b share a sign, +b adjustment otherwise. Tests in 71-quot-rem-mod
and 72-min-max-abs (previously red) cover all sign combinations.
Diffstat:
1 file changed, 14 insertions(+), 0 deletions(-)
diff --git a/scheme1/prelude.scm b/scheme1/prelude.scm
@@ -16,6 +16,20 @@
(define (abs x) (if (< x 0) (- 0 x) x))
+(define (min a b) (if (< a b) a b))
+(define (max a b) (if (< a b) b a))
+
+;; modulo has the sign of the divisor; remainder has the sign of the
+;; dividend. They differ exactly when r is nonzero and r and b have
+;; opposite signs -- in that case adjust by adding b.
+(define (modulo a b)
+ (let ((r (remainder a b)))
+ (if (zero? r)
+ 0
+ (if (eq? (negative? r) (negative? b))
+ r
+ (+ r b)))))
+
;; --- Common c*r compositions ---------------------------------------
(define (caar x) (car (car x)))
(define (cadr x) (car (cdr x)))