sicp 2.1

改进版有理数构造函数
本文介绍了一个改进版的make-rat函数,该函数能够处理正数和负数参数,并确保有理数规范化:若为正数,则分子和分母均为正;若为负数,则仅分子为负。

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.

 

(define (make-rat n d)
  (cond ((and (> n 0) (> d 0)) (cons n d))
        ((and (< n 0) (< d 0)) (cons (- n) (-d )))
        (else (cons (- (abs n)) (abs d)))))

(define (numer x)
  (car x))

(define (denom x)
  (cdr x))
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值