Sunday, April 4, 2010

SICP Series-chapter 1.3.3 and Chapter 1 review

In this section, the textbook focus on that make the function as return value.
The title for chapter 1 is Building Abstraction With Procedures, so procedure abstraction is the key on this chapter. If you are not familiar with some dynamic language like Python, JavaScript or Perl, you may feel surprised to see that we use the function as parameter or return value. Actually in some languages,function is called first-class object, which means function with the fewest restrictions, like the int, float. Some of the right of the first-class element are:

  • They may be named by variable
  • They may be passed as parameter
  • They may be returned as the result of procedures
  • They may be included in the data structure

Another important is the abstraction. In chapter one, we focus on the procedure, next chapter we will focus on the data abstraction.
Here is some functions that implemented in the textbook would be used in the exercise.

(defvar dx 0.00001)
 
(defun deriv(g)
    (lambda(x)
            (/ (- (funcall g (+ x dx)) (funcall g x))
               dx)))
 
(defun Newton-transform(g)
    (lambda(x)
           (- x (/ (funcall g x) (funcall (deriv g) x)))))
 
(defun newton-methond(g guess)
    (fixed-point (Newton-transform g) guess))
1.40
(defun cubic(a b c)
  (lambda(x) (+ (expt x 3)
                (* a (expt x 2))
                (* b x)
                c)))
1.42
(defun compose(g f)
    (lambda(x)
           (funcall g (funcall f x))))
1.43
(defun repeated(f n)
    (if (= n 0)
        f
        (compose f (repeated f (- n 1)))))
1.44
(defun smooth(f)
    (lambda(x)
        (/ (+ (funcall f x)
              (funcall f (+ x dx))
              (funcall f (- x dx)))
           3)))
 
(defun smoothN(f n)
    (funcall (repeated #'smooth n) f))

No comments:

Post a Comment