(defun int-to-binary (alist)
(setq n (- length(alist) 1))
(setq result 0)
(loop for x in alist
do(setq result (+ result (* x (expt 2 n)))
(setq n (- n 1))
)
)
(format t "~d" result)
(int-to-binary '(1 0 1 1))
报错如下:
*** - SYSTEM::%EXPAND-FORM: (SETQ RESULT (+ RESULT (* X (EXPT 2 N)))) should be a lambda expression
这个程序是要解决如下问题的:
Write a LISP function binary-to-int that converts a binary number that is given as list
comprising only 0 and 1 into a integer. For example:
(binary-to-int ’()) returns nil, because the empty set is not a representation of a binary number.
(binary-to-int ’(1 0)) returns 2
(binary-to-int ’(0 1)) returns 1
(binary-to-int ’(0)) returns 0
(binary-to-int ’(1 1 1 0)) returns 14