如影随形 魔法禁忌

(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)))))))
      
发布了40 篇原创文章 · 获赞 7 · 访问量 1083

猜你喜欢

转载自blog.csdn.net/BobDay/article/details/103797104
今日推荐