i decided reading sicp little, see (i not mit student, done studying, no homework, content might homework someone.). since have installed sbcl, have change syntax little, compared book. however, don't understand why solution exercise 1.3 not working:
(defun square (x) (* x x)) (defun sum-of-squares (x y) (+ (square x) (square y))) (defun sum-of-squares-two-max (x y z) ( (cond ((eq (min x y z) x) (sum-of-squares y z)) ((eq (min x y z) y) (sum-of-squares x z)) (t (sum-of-squares x y)) )))
to load run sbcl --load exercise-1.3.lisp
. when load it, error:
; file: /home/xiaolong/development/lisp/sicp/exercise-1.3.lisp ; in: defun sum-of-squares-two-max ; ((cond ((eq (min x y z) x) (sum-of-squares y z)) ; ((eq (min x y z) y) (sum-of-squares x z)) (t (sum-of-squares x y)))) ; ; caught error: ; illegal function call ; (defun sum-of-squares-two-max (x y z) ; ((cond ((eq # x) (sum-of-squares y z)) ((eq # y) (sum-of-squares x z)) ; (t (sum-of-squares x y))))) ; ; caught style-warning: ; variable x defined never used. ; ; caught style-warning: ; variable y defined never used. ; ; caught style-warning: ; variable z defined never used. ; ; compilation unit finished ; caught 1 error condition ; caught 3 style-warning conditions
multiple things don't understand:
- why variables not used? use of them in conditional ...
- why function call illegal? how can compose functions if cannot call function inside function? (i can, doing wrong?)
when comment out third function, loads without errors.
i checked syntax conditional multiple times already, cannot find mistake.
how can correct code?
get rid of open paren.
(defun sum-of-squares-two-max (x y z) (cond ((eq (min x y z) x) (sum-of-squares y z)) ((eq (min x y z) y) (sum-of-squares x z)) (t (sum-of-squares x y))))
the issue that, paren, code call whatever cond
evaluate (as first items within parens). without paren, knows result of function whatever cond
evaluates to.
also, general warning: sicp uses scheme, you'll see small differences between book , common lisp macros force into.
Comments
Post a Comment