summaryrefslogtreecommitdiffstats
path: root/net/ricketyspace/sicp/two/one.scm
diff options
context:
space:
mode:
Diffstat (limited to 'net/ricketyspace/sicp/two/one.scm')
-rw-r--r--net/ricketyspace/sicp/two/one.scm22
1 files changed, 22 insertions, 0 deletions
diff --git a/net/ricketyspace/sicp/two/one.scm b/net/ricketyspace/sicp/two/one.scm
new file mode 100644
index 0000000..5d184dc
--- /dev/null
+++ b/net/ricketyspace/sicp/two/one.scm
@@ -0,0 +1,22 @@
+;;;; Under Creative Commons Attribution-ShareAlike 4.0
+;;;; International. See
+;;;; <https://creativecommons.org/licenses/by-sa/4.0/>.
+
+(define-module (net ricketyspace sicp two one)
+ #:export (make-rat))
+
+(define (gcd a b)
+ (if (= b 0)
+ a
+ (gcd b (remainder a b))))
+
+(define (make-numer n neg?)
+ (if neg? (* -1 (abs n))
+ (abs n)))
+(define (make-denom d) (abs d))
+
+(define (make-rat n d)
+ (let ((g (abs (gcd n d)))
+ (neg? (< (* n d) 0)))
+ (cons (/ (make-numer n neg?) g)
+ (/ (make-denom d) g))))