Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.57代码不能对乘0和乘1操作进行化简,还产生了加号或乘号之后只有一个操作数的情况 #19

Open
Perry961002 opened this issue Dec 5, 2018 · 0 comments

Comments

@Perry961002
Copy link

我的不成熟的代码:
;重新定义make-sum过程,接受参数列表, 并整合成一个和式
(define (make-sum . items)
(define (iter result x)
(if (null? x)
result
(let ((first (car x)))
(if (=number? first 0)
(iter result (cdr x))
(iter (append result (list first))
(cdr x))))))
(let ((add (iter '() items)))
(if (null? (cdr add))
(car add)
(append '(+) add))))
;重新定义过程make-product, 接受参数列表, 并整合成一个乘式
(define (make-product . items)
(define (iter result x)
(if (null? x)
result
(let ((first (car x)))
(cond ((=number? first 0) (list 0))
((=number? first 1)
(iter result (cdr x)))
(else (iter (append result (list first))
(cdr x)))))))
(let ((mul (iter '() items)))
(if (null? (cdr mul))
(car mul)
(append '(*) mul))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant