SICP-2.3.1节练习
SICP-2.3.3节练习

SICP-2.3.2节练习

lispor posted @ Apr 28, 2011 03:16:03 AM in Scheme with tags SICP , 2582 阅读

 

练习 2.56 - 2.58
 
练习 2.56:
Show how to extend the basic differentiator to handle more kinds of expressions. For instance,
implement the differentiation rule
n_1   n_2
--- = ---  if and only if n_1 d_2 = n_2 d_1
d_1   d_2
by adding a new clause to the deriv program and defining appropriate procedures exponentiation?,
base, exponent, and make-exponentiation. (You may use the symbol ** to denote exponentiation.) Build
in the rules that anything raised to the power 0 is 1 and anything raised to the power 1 is the
thing itself.
我的解答:
(define (variable? x)
  (symbol? x))

(define (same-variable? v1 v2)
  (and (variable? v1)
       (variable? v2)
       (eq? v1 v2)))

(define (=number? exp num)
  (and (number? exp)
       (= exp num)))

(define (make-sum a1 a2)
  (cond ((=number? a1 0) a2)
        ((=number? a2 0) a1)
        ((and (number? a1) (number? a2))
         (+ a1 a2))
        (else
         (list '+ a1 a2))))

(define (sum? x)
  (and (pair? x)
       (eq? (car x) '+)))

(define (addend s)
  (cadr s))

(define (augend s)
  (caddr s))

(define (make-product m1 m2)
  (cond ((or (=number? m1 0) (=number? m2 0))
         0)
        ((=number? m1 1) m2)
        ((=number? m2 1) m1)
        ((and (number? m1) (number? m2))
         (* m1 m2))
        (else
         (list '* m1 m2))))

(define (product? x)
  (and (pair? x)
       (eq? (car x) '*)))

(define (multiplier p)
  (cadr p))

(define (multiplicand p)
  (caddr p))

(define (make-exponentiation base exponent)
  (cond ((=number? exponent 0) 1)
        ((=number? exponent 1) base)
        (else
         (list '** base exponent))))

(define (exponentitation? x)
  (and (pair? x)
       (eq? (car x) '**)))

(define (base e)
  (cadr e))

(define (exponent e)
  (caddr e))

(define (deriv exp var)
  (cond ((number? exp) 0)
        ((variable? exp)
         (if (same-variable? exp var) 1 0))
        ((sum? exp)
         (make-sum (deriv (addend exp) var)
                   (deriv (augend exp) var)))
        ((product? exp)
         (make-sum (make-product (multiplier exp)
                                 (deriv (multiplicand exp) var))
                   (make-product (deriv (multiplier exp) var)
                                 (multiplicand exp))))
        ((exponentitation? exp)
         (make-product (exponent exp)
                       (make-product (make-exponentiation (base exp)
                                                          (- (exponent exp) 1))
                                     (deriv (base exp) var))))
        (else
         (error "unknown expression type -- DERIV" exp))))
 
练习 2.57:
Extend the differentiation program to handle sums and products of arbitrary numbers of (two or more)
terms. Then the last example above could be expressed as
(deriv '(* x y (+ x 3)) 'x)
Try to do this by changing only the representation for sums and products, without changing the deriv
procedure at all. For example, the addend of a sum would be the first term, and the augend would be
the sum of the rest of the terms.
我的解答:
(define (variable? x)
  (symbol? x))

(define (same-variable? v1 v2)
  (and (variable? v1)
       (variable? v2)
       (eq? v1 v2)))

(define (=number? exp num)
  (and (number? exp)
       (= exp num)))

(define (make-sum . as)
  (cond ((null? as) 0)
        ((=number? (car as) 0) (apply make-sum (cdr as)))
        ((null? (cdr as)) (car as))
        ((and (number? (car as)) (number? (cadr as)))
         (apply make-sum (cons (+ (car as)
                                  (cadr as))
                               (cddr as))))
        (else
         (cons '+ as))))

(define (sum? x)
  (and (pair? x)
       (eq? (car x) '+)))

(define (addend s)
  (cadr s))

(define (augend s)
  (apply make-sum (cddr s)))

(define (make-product . ms)
  (cond ((null? ms) 1)
        ((=number? (car ms) 0) 0)
        ((=number? (car ms) 1)
         (apply make-product (cdr ms)))
        ((null? (cdr ms)) (car ms))
        ((and (number? (car ms)) (number? (cadr ms)))
         (apply make-product (cons (* (car ms)
                                      (cadr ms))
                                   (cddr ms))))
        (else
         (cons '* ms))))

(define (product? x)
  (and (pair? x)
       (eq? (car x) '*)))

(define (multiplier p)
  (cadr p))

(define (multiplicand p)
  (apply make-product (cddr p)))

(define (make-exponentiation base exponent)
  (cond ((=number? exponent 0) 1)
        ((=number? exponent 1) base)
        (else
         (list '** base exponent))))

(define (exponentitation? x)
  (and (pair? x)
       (eq? (car x) '**)))

(define (base e)
  (cadr e))

(define (exponent e)
  (caddr e))

(define (deriv exp var)
  (cond ((number? exp) 0)
        ((variable? exp)
         (if (same-variable? exp var) 1 0))
        ((sum? exp)
         (make-sum (deriv (addend exp) var)
                   (deriv (augend exp) var)))
        ((product? exp)
         (make-sum (make-product (multiplier exp)
                                 (deriv (multiplicand exp) var))
                   (make-product (deriv (multiplier exp) var)
                                 (multiplicand exp))))
        ((exponentitation? exp)
         (make-product (exponent exp)
                       (make-product (make-exponentiation (base exp)
                                                          (- (exponent exp) 1))
                                     (deriv (base exp) var))))
        (else
         (error "unknown expression type -- DERIV" exp))))
 
练习 2.58:
Suppose we want to modify the differentiation program so that it works with ordinary mathematical
notation, in which + and * are infix rather than prefix operators. Since the differentiation program
is defined in terms of abstract data, we can modify it to work with different representations of
expressions solely by changing the predicates, selectors, and constructors that define the
representation of the algebraic expressions on which the differentiator is to operate.
Show how to do this in order to differentiate algebraic expressions presented in infix form, such as
(x + (3 * (x + (y + 2)))). To simplify the task, assume that + and * always take two arguments and
that expressions are fully parenthesized.
The problem becomes substantially harder if we allow standard algebraic notation, such as (x + 3 *
(x + y + 2)), which drops unnecessary parentheses and assumes that multiplication is done before
addition. Can you design appropriate predicates, selectors, and constructors for this notation such
that our derivative program still works?
我的解答:
a:
(define (variable? x)
  (symbol? x))

(define (same-variable? v1 v2)
  (and (variable? v1)
       (variable? v2)
       (eq? v1 v2)))

(define (=number? exp num)
  (and (number? exp)
       (= exp num)))

(define (make-sum a1 a2)
  (cond ((=number? a1 0) a2)
        ((=number? a2 0) a1)
        ((and (number? a1) (number? a2))
         (+ a1 a2))
        (else
         (list a1 '+ a2))))

(define (sum? x)
  (and (pair? x)
       (not (null? (cdr x)))
       (eq? (cadr x) '+)))

(define (addend s)
  (car s))

(define (augend s)
  (caddr s))

(define (make-product m1 m2)
  (cond ((or (=number? m1 0) (=number? m2 0))
         0)
        ((=number? m1 1) m2)
        ((=number? m2 1) m1)
        ((and (number? m1) (number? m2))
         (* m1 m2))
        (else
         (list m1 * m2))))

(define (product? x)
  (and (pair? x)
       (not (null? (cdr x)))
       (eq? (cadr x) '*)))

(define (multiplier p)
  (car p))

(define (multiplicand p)
  (caddr p))

(define (make-exponentiation base exponent)
  (cond ((=number? exponent 0) 1)
        ((=number? exponent 1) base)
        (else
         (list '** base exponent))))

(define (exponentitation? x)
  (and (pair? x)
       (eq? (car x) '**)))

(define (base e)
  (cadr e))

(define (exponent e)
  (caddr e))

(define (deriv exp var)
  (cond ((number? exp) 0)
        ((variable? exp)
         (if (same-variable? exp var) 1 0))
        ((sum? exp)
         (make-sum (deriv (addend exp) var)
                   (deriv (augend exp) var)))
        ((product? exp)
         (make-sum (make-product (multiplier exp)
                                 (deriv (multiplicand exp) var))
                   (make-product (deriv (multiplier exp) var)
                                 (multiplicand exp))))
        ((exponentitation? exp)
         (make-product (exponent exp)
                       (make-product (make-exponentiation (base exp)
                                                          (- (exponent exp) 1))
                                     (deriv (base exp) var))))
        (else
         (error "unknown expression type -- DERIV" exp))))

b:
还不会

 

deep cleaning abu dh 说:
Feb 21, 2020 10:34:30 PM

For the name denotes, deep housecleaning represents another degree cleaning " up " your office or home. Basic housecleaning includes general pick-up like wiping and additionally vacuuming your kitchen, bathroom, bedroom and living room space floors, combined with cleaning all the cupboard side, tables, bar stools, and the like, using fluids or almost every basic housecleaning products; still, deep cleaning is related to paying attention to the highlights and eradicating hidden airborne dust, dirt, and additionally small marks. It is mostly a top to make sure you bottom scrubbing of your sarasota home, from ceilings to make sure you floors, Furnished deep housecleaning include housecleaning what’s within tiles, behind the automatic washer, under all the sink, around the oven, cleaning my tv room corners…etc.

maid services dubai 说:
Feb 22, 2020 06:15:27 PM

The very last thing make sure you check for before choosing a carpet cleaning company is definitely they method of carpet cleaning up equipment plus methods how they use just in case they specialise in any one specific type with service. You ought to ask once they do understand your specific method of rugs plus carpets just in case they use the right procedure for cleaning for anything you have. The very last thing you wish is for your personal Oriental carpeting or Local carpets that they are ruined wish company used a different carpet cleaning up process and also chemicals.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter