"SICP Exercise" 98 Exercise 2.73

Source: Internet
Author: User
Tags mul

Exercise 2.73

A small problem, this is because the scheme of the number, the direct provision of variables, the former will be treated as a numeric type, the latter will be used as the symbol type. Therefore, it is not necessary to add these two predicates to the data-driven dispatch. If you add a flag to a numeric type or symbol type, it is not a waste of space or time to judge a known type in the get process.
b, we have completed the problem according to the examples we have in the book, which is the top of the 123th page and the 125th page.

(Define (install-sum-package)  (define (addend s) (car s))       (define (augend s) (cadr s))       (Define (make-sum x y)          (cons ((=numberx 0) y) ((=numbery 0) x)((number x) (number y))   (+ x y))  (else(attach ' Add x y)                                 ))      (put ' addend ' Add Addend)      (put ' augend ' add augend)      (put ' make-sum ' add make-sum)  (put ' deriv ' Add (lambda (exp var)(make-sum 
        
          (
         deriv 
         (addend exp) var)
         (deriv (augend exp) var 
                        ))))' Done '); Value:install-sum-package(define (make-sum x y)((get ' make-sum ' Add) x y))  ; Value:make-sum(define (addend sum)((get ' addend ' Add) (   Contents sum   ))); Value:addend(define (augend sum)((get ' augend ' Add) ( contents Sum   ))); Value:augend(install-sum-package); Unbound Variable:put; To continue, call RESTART with a option number:; (RESTART 8) = Specify a value to use instead of put.; (RESTART 7) = Define put to a given value.; (RESTART 6) = Return to Read-eval-print level 6.; (RESTART 5) = Return to Read-eval-print level 5.; (RESTART 4) = Return to Read-eval-print level 4.; (RESTART 3) = Return to Read-eval-print level 3.; (RESTART 2) = Return to Read-eval-print level 2.; (RESTART 1) = Return to Read-eval-print level 1.; Start debugger? (Y or N): N

It was not until this step was written that I found that the put was undefined and not defined. So this problem, I used to put it down until I wrote the putting and get through the later study.

(define operation-table (make-table))(define put (operation-table ‘insert-proc!))(define get (operation-table ‘lookup-proc))

The insert-proc in the put, plus an exclamation point, is the norm in scheme--adding "!" to a destructive operation. and Make-table in the book on the 186th page and 187 pages have a definition, did not learn it does not matter, here again to use a legendary according to Desire thinking.
B also asked us to write about the derivation of the accumulation of the process, and then also to install into the table.

(Define (install-product-package)    (define (multiplier P ) (car P))    (define (multiplicand P ) (cadr P))    (Define (make-product x y)        (Cond ((or (=number x 0) (=number y 0))  0)               ((=number x 1) y)               ((=number y 1) x)               (( number x  ) (number y) )(* x Y) )                (else(attach-tag ' Mul x y)) ))      (put ' multiplier ' mul multiplier)      (put ' Multiplicand ' mul multiplicand)       (put  ' make-product ' mul make-product)             (put  ' Deriv ' mul  lambda   (exp  var)  
       
         (
        make-sum  
         ( make-product   (multiplier  exp)   (deriv   ( Multiplicand  exp)  var) )  
         (make-product  deriv   (multiplier  exp)  var)  
         (multiplicand  exp) ) 
       ) ) ) ' Done ')     (define (make-product x y)((get ' make-product ' mul) x y))               (define (multiplier product)((get ' multiplier ' Mul) (contents product)))                 (define (multiplicand product)((get ' Multiplicand ') (  Contents Product            )))

Note that the previous Attach-tag does not apply here, because the parameter to pass in has two operands other than an operator and is no longer one.

(define (attach-tag type-tag x y)(list type-tag x y))(define (type-tag datumn)(car datumn))(define (type-tag datumn)(cdr datumn))

This is really a long topic ah, C small question reference to the previous two code written should be very easy, if do not know how to do this algorithm, in exercise 2.56 we have encountered.
The last question, the topic gives another kind of get form, but already written the package will not have to be modified, because directly change put can. And the changes in get are just the order. So the change of put is just the order of change.

(put ‘make-sum ‘add make-sum)(put ‘add ‘make-sum make-sum)

The previous one was written before, and the latter one was after the minor problem was changed.

"SICP Exercise" 98 Exercise 2.73

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.