(define numbered?
(lambda (aexp)
(cond
((atom? aexp)(number? aexp))
((eq?(car(cdr aexp))(quote +))
(and(numbered?(car aexp))
(numbered?
(car(cdr(cdr aexp))))))
((eq?(car(cdr aexp))(quote *))
(and(numbered?(car aexp))
(numbered?
(car(cdr(cdr aexp))))))
((eq?(car(cdr aexp))(quote →))
(and(numbered?(car aexp))
(and(numbered?(car aexp))
(numbered?
(car(cdr(cdr aexp))))))))))
;由于aexp已经被看作一个表达式,简化numbered?
(define numbered1?
(lambda(aexp)
(cond
((atom? aexp)(numbered1? aexp))
(else
(and(numbered1?(car aexp))
(numbered1?
(car(cdr(cdr aexp)))))))))
(define value
(lambda(nexp)
(cond
((atom? nexp)nexp)
((eq?(car(cdr nexp))(quote +))
(+ (value(car nexp))
(value(car(cdr(cdr nexp))))))
((eq?(car(cdr nexp))(quote *))
(* (value(car nexp))
(value(car(cdr(cdr nexp))))))
(else
(→ (value(car nexp))
(value
(car(cdr(cdr nexp)))))))))
;一个算术表达式的表示方式的第一个子表达式
(define 1st-sub-exp
(lambda(aexp)
(car(cdr aexp))))
(define 2nd-sub-exp
(lambda(aexp)
(car(cdr(cdr aexp)))))
(define operator
(lambda(aexp)
(car aexp)))
;重写value
(define value1
(lambda(nexp)
(cond
((atom? nexp)nexp)
((eq?(operator nexp)(quote +))
(+(value1(1st-sub-exp nexp))
(value1(2nd-sub-exp nexp))))
((eq?(operator nexp)(quote *))
(*(value1(1st-sub-exp nexp))
(value1(2nd-sub-exp nexp))))
(else
(→(value1(1st-sub-exp nexp))
(value1(2nd-sub-exp nexp)))))))
;一种新的数字表示法()表示零,(())表示1,(()())表示2,(()()())表示3
(define sero?
(lambda(n)
(null? n)))
(define edd1
(lambda(n)
(cons(quote())n)))
(define zub1
(lambda(n)
(cdr n)))
(define +
(lambda(n m)
(cond
((sero? m)n)
(else(edd1(+ n(zub1 m)))))))
如影随形 魔法禁忌
最新推荐文章于 2024-09-07 09:01:31 发布