sicp每日一题[2.1]
1.sicp每日一题[1.42]2.sicp每日一题[1.43]3.sicp每日一题[1.44]4.sicp每日一题[1.45]5.sicp每日一题[1.46]
6.sicp每日一题[2.1]
7.sicp每日一题[2.2]8.sicp每日一题[2.3]9.sicp每日一题[2.4]10.sicp每日一题[2.5]11.sicp每日一题[2.6]12.sicp每日一题[2.7]13.sicp每日一题[2.8]14.sicp每日一题[2.9]15.sicp每日一题[2.11]16.sicp每日一题[2.10]17.sicp每日一题[2.12]18.sicp每日一题[2.13-2.16]19.sicp每日一题[2.17]20.sicp每日一题[2.18]21.sicp每日一题[2.19]22.sicp每日一题[2.20]23.sicp每日一题[2.21]24.sicp每日一题[2.22-2.23]25.sicp每日一题[2.24-2.27]26.sicp每日一题[2.28]27.sicp每日一题[2.29]28.sicp每日一题[2.30]29.sicp每日一题[2.31]30.sicp每日一题[2.32]31.sicp每日一题[2.33]32.sicp每日一题[2.34]33.sicp每日一题[2.35]34.sicp每日一题[2.36-2.37]35.sicp每日一题[2.38-2.39]36.sicp每日一题[2.40]37.sicp每日一题[2.41]38.sicp每日一题[2.42]39.sicp每日一题[2.43]40.sicp每日一题[2.44]41.sicp每日一题[2.45]42.sicp每日一题[2.46]43.sicp每日一题[2.47]44.sicp每日一题[2.48]45.sicp每日一题[2.49]46.sicp每日一题[2.50]47.sicp每日一题[2.51]48.sicp每日一题[2.52]49.sicp每日一题[2.53-2.55]50.sicp每日一题[2.56]51.sicp每日一题[2.57]52.sicp每日一题[2.58]53.sicp每日一题[2.59]54.sicp每日一题[2.60]55.sicp每日一题[2.61]56.sicp每日一题[2.62]57.sicp每日一题[2.63-2.64]58.sicp每日一题[2.65]59.sicp每日一题[2.66]60.sicp每日一题[2.67-2.68]61.sicp每日一题[2.69]62.sicp每日一题[2.70]63.sicp每日一题[2.71]64.sicp每日一题[2.72]65.sicp每日一题[2.73]66.sicp每日一题[2.74]67.sicp每日一题[2.75]68.sicp每日一题[2.76]69.sicp每日一题[2.77]70.sicp每日一题[2.78]71.sicp每日一题[2.79]72.sicp每日一题[2.80]73.sicp每日一题[2.81]74.sicp每日一题[2.82]75.sicp每日一题[2.83]76.sicp每日一题[2.84]77.sicp每日一题[2.85-2.86]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 就可以实现,不过我暂时不改了,也许后面有道题目会让做这个事情,到时候再说。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器