SICP section 1.1

Posted on 2015-02-12 04:51:53 +0900 in SICP Lisp

1.3

In order to find out the bigger ones, compare them pairwisely.

#lang planet neil/sicp

(define (square x)
  (* x x))

(define (sum-of-squares x y)
    (+ (square x)
       (square y)))

(define (bigger x y)
  (if (> x y) x y))

(define (smaller  x y)
  (if (< x y) x y))

(define (bigger-sum-of-square x y z)
  (sum-of-squares (bigger x y)
                 (bigger (smaller x y)
                         z)))

1.4

This is the first order useage of the function, which is +

#lang planet neil/sicp

(define (a-plus-abs-b a b)
  ((if (> b 0) + -) a b))
  

1.5

#lang planet neil/sicp

(define (p) (p))
(define (test x y)
(if (= x 0) 0 y))

If it is evaluated in applicative order, then the evaluation of the (p) would lead to infinite loop. While, if it is evaluated in normal order, then 0 would return before it is evaluated.

1.6

(if  predicate   consequent   alternative )

If is special form becuase it is not a regular expression, which will have its arguments being evaluted at first.

Onlyconsequent OR alternative will be evaluted, but never both at the same time.

The new-if statement will lead to infinite loop.

1.7

For small numbers, such as (sqrt 0.00000000000002), the accuracy itself is too inaccurate.

For large numbers, such as (sqrt 20000000000000) will be trapped in infinite loop. The guess would be a fixed point which equals 4472135.954995. But 20000000000000/4472135.954995 =4472135.955 which is not equal to 4472135.954995 exactly.

The reason is for float number, when the exp bits gets larger, its coificent bits would loose some accuracy. Accurary lost meet with big exponent number, then this float number becomes inaccurate.

----------------------------------- 本文内容遵从CC版权协议转载请注明出自kamelzcs -----------------------------------
«  | 说好的2014 »

Hide Comments

comments powered by Disqus