In the section, it gives some very basic introduction about the LISP(a familiar of LISP, mainly Schema in this book). Like the basic expression, naming ,environment and other element of language.
1.4. The common lisp has some difference with Schema, so I re-write the small program in the exercise. Actually, you can use the function name as you like, just like the example in the exercise. But in the Common Lisp, if you want to tell the interpreter that a name is a function , you need add a key word function. If you want to call a function based on a symbol, you need use the funcall key word to tell the interpreter, you also can use the #’ to instead of funcall.
(defun a-plus-b-v2 (a b)
(funcall (if (> b 0)
(function +)
(function -))
a
b))
1.5 From the code, you can easily know that the p is a infinite recursive function. If the test function return 0, it means normal order, the p would be evaluated until it’s actually needed.
From the screenshot, we can tell that it’s normal order.
1.6 It would cause a infinite function call.
1.7 According to the exercise, the good-enough? function may cause big problem when the number is very small. From the code we can see that it use the guess number’s square to compare the original number, the comparison number is o.oo1. When the number is larger, it’s ok. But when the number is very small, like 0.06 or 0.005, it would cause big inaccuracy. In order to decrease the inaccuracy, there is a strategy that compare last iteration’s guess with current guess.
(let (improveguess (improve guess x))
(if (good-enoughp guess improveguess)
improveguess
(sqrt-iter (improve improveguess x)
x))))
(defun improve (guess x)
(average guess (/ x guess)))
(defun average (x y)
(/ (+ x y) 2))
(defun good-enoughpv2 (guess x)
(let (temp (/ guess x))
(and (> temp 0.99) (< temp 1.01))))
(defun sqrtv(x)
(sqrt-iter 1.0 x))
1.8 This is the same as the example in the textbook. The only different is the improve-guess function
No comments:
Post a Comment