当前位置: 首页 > article >正文

sicp每日一题[2.1]

Exercise 2.1

Exercise 2.1: Define a better version of m a k e − r a t make-rat makerat that handles both positive and negative arguments. m a k e − r a t make-rat makerat 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 就行,利用 g c d gcd gcd 就可以实现,不过我暂时不改了,也许后面有道题目会让做这个事情,到时候再说。


http://www.kler.cn/news/294582.html

相关文章:

  • docker 容器
  • 数据库 | 子查询 | sql执行顺序 | mysql是否运行
  • AI文献综述神器,有这一款就够了!
  • 《JavaEE进阶》----11.<SpringIOCDI【Spring容器+IOC详解+DI介绍】>
  • tcp 流量控制
  • 开发一款通过蓝牙连接控制水电表的微信小程序
  • ubuntu 安装python3 教程
  • 开篇_____何谓安卓机型“工程固件” 与其他固件的区别 作用
  • springboot websocket 服务端
  • 用 Python 编写桌面时钟程序
  • MATLAB绘图基础5:MATLAB数据导入
  • Redis总结,是什么,干什么,怎么利用?
  • element table 表格 span-method 某一列进行相同合并 支持树结构表格
  • 数据结构基础之《(2)—对数器》
  • 海外直播对网速、带宽、安全的要求
  • Docker容器创建时,无法访问镜像源:Could not connect to archive.ubuntu.com:80
  • C语言操作符详解1(含进制转换,原反补码)
  • 消息队列 think-queue tp5.0
  • 使用Docker启动Redis容器并映射端口
  • ChatGPT3.5/4.0新手使用手册,国内中文版使用教程
  • 记录 vue-router 跳转到第一个有权限的菜单的实现方式
  • 【大模型开发】传统向量模型 vs 重排序模型:原理、实现与应用
  • 欺诈文本分类检测(十二):模型导出与部署
  • 八大排序原来也不过如此
  • 【软件逆向】第27课,软件逆向安全工程师之(二)寄存器寻址,每天5分钟学习逆向吧!
  • 【MIT 6.5840/6.824】In Search of an Understandable Consensus Algorithm 学习笔记
  • 如何使用useMemo来优化React组件的性能?
  • 7、关于LoFTR
  • 三维布尔运算对不规范几何数据的兼容处理
  • Linux 中常用的 Vim 命令大全