sicp每日一题[2.1]

Exercise 2.1

Exercise 2.1: Define a better version of make-rat that handles both positive and negative arguments. make-rat should normalize the sign so that if the rational number is positive, both the numerator and denominator are positive, and if the rational number is negative, only the numerator is negative.


第一章学完了,今天开始学习第二章,目前还没有遇到什么问题,这道题也比较简单,只要注意到"分子分母同时为正,或者分子为负,分母为正,不需要改变符号;分子分母同时为负,或者分子为正,分母为负,分子分母都需要改变符号"这一点,就可以很容易地实现题目要求。

复制代码
; (make-rat ⟨n⟩ ⟨d ⟩) returns the rational number whose numerator is the integer ⟨n⟩ and whose denominator is the integer ⟨d ⟩.
(define (make-rat n d)
  (let ((n (/ n (gcd n d)))
        (d (/ d (gcd n d))))
    ; 分子分母同时为正,或者分子为负,分母为正,不需要改变符号
    ; 分子分母同时为负,或者分子为正,分母为负,分子分母都需要改变符号
    (if (or (and (positive? n) (positive? d))
            (and (negative? n) (positive? d)))
        (cons n d)
        (cons (- 0 n) (- 0 d)))))

; (numer ⟨x⟩) returns the numerator of the rational number ⟨x⟩.
(define (numer x) (car x))

; (denom ⟨x⟩) returns the denominator of the rational number ⟨x⟩.
(define (denom x) (cdr x))

(define (add-rat x y)
  (make-rat (+ (* (numer x) (denom y))
               (* (numer y) (denom x)))
            (* (denom x) (denom y))))

(define (sub-rat x y)
  (make-rat (- (* (numer x) (denom y))
               (* (numer y) (denom x)))
            (* (denom x) (denom y))))

(define (mul-rat x y)
  (make-rat (* (numer x) (numer y))
            (* (denom x) (denom y))))

(define (div-rat x y)
  (make-rat (* (numer x) (denom y))
            (* (denom x) (numer y))))

(define (equal-rat? x y)
  (= (* (numer x) (denom y))
     (* (numer y) (denom x))))

(define (print-rat x)
  (newline)
  (display (numer x))
  (display "/")
  (display (denom x)))


(print-rat (make-rat -1 2))
(print-rat (make-rat 1 2))

(define neg-one-half (make-rat -1 2))
(define one-third (make-rat 1 3))

(print-rat (add-rat neg-one-half one-third))
(print-rat (add-rat neg-one-half neg-one-half))
(print-rat (sub-rat neg-one-half one-third))
(print-rat (mul-rat neg-one-half one-third))
(print-rat (div-rat neg-one-half one-third))

; 执行结果
-1/2
1/2
-1/6
-1/1
-5/6
-1/6
-3/2

可以看出这里还是有改进空间,-1/1 写成 -1 就行,利用 gcd 就可以实现,不过我暂时不改了,也许后面有道题目会让做这个事情,到时候再说。

相关推荐
codigger9 小时前
AI 写代码越来越快,我们的代码能力却在悄悄退化
程序员·编程·ai编程·#人工智能
noravinsc1 天前
vibe coding 是什么?
ai·编程
小贺儿开发2 天前
DeepSeek + Codex + MCP:Unity 游戏开发实测
科技·游戏·unity·ai·程序员·编程·deepseek
aluluka4 天前
《elisp 每日一练》—— syntax table 与 font lock
emacs·lisp·elisp
codigger4 天前
Java 26 深度解析:一次“性能兑现 + 契约收紧”的版本
java·后端·编程·编程语言
skywalk81637 天前
光明语言入榜计划:GitHub Linguist 注册指南・光明语言模块参考
人工智能·编程·光明
白猫不黑8 天前
网络安全专业:从入门到进阶的完整学习路线
学习·安全·web安全·计算机·网络安全·信息安全·编程
codigger9 天前
从 "调模型" 到 "搭系统":Harness Engineering 凭什么成为 2026 年 AI 圈最热的新词
程序员·编程·#人工智能·#agent
青柠之夏cc9 天前
掌握自动化,高效工作新引擎
编程·脚本·语法
codigger10 天前
把 AI Agent 养在自己电脑上:从本地部署到远程接管的一份完整思路
ai·编程·#人工智能·#agent