Saturday, April 17, 2010

SICP Series-chapter 2.3

I don’t like the section of 2.2.4, so I skip them.

Until now, all the data we deal with is number, integer or float. Now we will face symbolic data. What’s symbolic data? For example. What’s the difference between these two sentences. “Please say your name” and “Plase say ‘your name’ “. If you and understand what’s the difference, you will know what’s symbolic data.

(defvar a 1) 
(defvar b 2)
(list a b) 
(list 'a 'b)

You can try the code above, and compare the results. You will find that ‘a stands for the character “a”, not the value 1. I don’t know how to use more rigorous definition to depict it. But I think you can get the main point from the code and example above.

2.53

2.54

(defun mySymListEq(a b)
 
(cond ((and (symbolp a)
             
(symbolp b)
             
(eq a b))
         t
)
       
((not (and (listp a) (listp b)))
         
nil)
       
(t (and (mySymListEq (car a) (car b))
               
(mySymListEq (cdr a) (cdr b))))))

2.55

‘ symbol is the abbreviation of quote


2..3.2 is a beautifully designed system. I like this section. At first, I need put the code here provided by the textbook first.


(defun make-sum (a1 a2)
 
(list '+ a1 a2))

(defun make-product (m1 m2)
  (list '
* m1 m2))

(defun sum? (x)
 
(and (consp x) (eql (car x) '+)))

(defun addend (s)
  (cadr s))

(defun augend (s)
  (caddr s))

(defun product? (x)
  (and (consp x) (eql (car x) '
*)))

(defun multiplier (s)
 
(cadr s))

(defun multiplicand (s)
 
(caddr s))

(defun deriv (expr var)
 
(cond ((numberp expr) 0)
       
((variable? expr)
         
(if (same-variable? expr var) 1 0))
       
((sum? expr)
         
(make-sum (deriv (addend expr) var)
                   
(deriv (augend expr) var)))
       
((product? expr)
         
(make-sum
           
(make-product (multiplier expr)
                         
(deriv (multiplicand expr) var))
           
(make-product (deriv (multiplier expr) var)
                         
(multiplicand expr))))
       
(t
         
(error "unknown expression type -- DERIV ~A" expr))))
(defun =number? (expr num)
 
(and (numberp expr) (= expr num)))

(defun make-sum (a1 a2)
 
(cond ((=number? a1 0) a2)
       
((=number? a2 0) a1)
       
((and (numberp a1) (numberp a2)) (+ a1 a2))
       
(t (list '+ a1 a2))))

(defun make-product (m1 m2)
  (cond ((or (=number? m1 0) (=number? m2 0)) 0)
        ((=number? m1 1) m2)
        ((=number? m2 1) m1)
        ((and (numberp m1) (numberp m2)) (* m1 m2))
        (t (list '
* m1 m2))))

2.56

(defun exponentiation?(expr) 
    (and (listp expr) (symbolp '** (car expr))))   
(defun base(expo-expr) 
    (cadr expo-expr))   
(defun exponent(expo-expr) 
    (caddr expo-expr))   
(defun make-exponentiation(base expo) 
    (cond ((= base 0) 0) 
              ((= expo 0) 1) 
              ((and (numberp base) (numberp expo)) (expt base expo)) 
              ((and (symbolp expo) (numberp base)) 
                    (error "At the moment, it do not support this formate")) 
             (t (list '** base expo))))   
;;This is the code snippet for exponentiation 
(if (exponentiation expr) 
        (make-product 
            (make-product (exponent expr) 
                                     (make-exponentiation (base expr) (- (exponent expr) 1))) 
    (deriv (base expr) var)))

2.57
I think this exercise is a good example of using the data abstract. If you have a good data-selector, user do not need know the detail about how to store the data.In this case,Only thing you need to change is the function which is used to get the proper data. You do not need to change the main function.


(defun make-sumv2(&rest numbers)
 
(append (list '+) numbers))

(defun augendv2(expr)
  (if (> (length expr) 3)
      (append (list '
+) (cddr expr))
     
(caddr expr)))

(defun make-productv2(&rest numbers)
 
(append (list '*) numbers))

(defun multiplicandv2(expr)
  (if (> (length expr) 3)
      (append (list '
*) (cddr expr))
     
(caddr expr)))

2.58
For the first part. it’s very easy. For the second part, I intend to finish later, not now.


No comments:

Post a Comment