值0不是STRING lisp错误类型

时间:2014-12-28 20:01:41

标签: lisp common-lisp

我是lisp的新手,我正在制作一个代码,通过使用sbcl移位和添加来增加两个数字。这是我的代码

    (
    defun calculator(num1 num2)
    (write-line "In the function")
    (setq res 0)
    (loop for lpr from 0 to 63
        do(progn
                (let ((end-bit (logand num2 1)))
                (if (= end-bit 1)
                    (+ res num1)
                )
                (ash num2 -1)
                (ash num1 1)
            ))
    )
    (write-line res)
)

(write-line "Enter first number : ")
(setq num1 (read))
(write-line "Enter second number : ")
(setq num2 (read))

;;(print num1)
;;(print num2)

(
    if(or (= num1 0) (= num2 0))
    (write-line "result is 0.0")
    (calculator num1 num2)
)

但我收到此错误并且不知道如何删除它。为什么会发生这种情况,请解释。

In the function

debugger invoked on a TYPE-ERROR in thread
#<THREAD "main thread" RUNNING {1002A8AF13}>:
  The value 0 is not of type STRING.

Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.

restarts (invokable by number or by possibly-abbreviated name):
  0: [ABORT] Exit debugger, returning to top level.

(WRITE-LINE 0 1) [tl,external]
0] 

1 个答案:

答案 0 :(得分:3)

请以通常的方式缩进和分隔您的代码。

(defun calculator (num1 num2)
  (write-line "In the function")
  (setq res 0)
  (loop for lpr from 0 to 63
        do (progn
             (let ((end-bit (logand num2 1)))
               (if (= end-bit 1)
                   (+ res num1))
               (ash num2 -1)
               (ash num1 1))))
  (write-line res))

(write-line "Enter first number : ")
(setq num1 (read))
(write-line "Enter second number : ")
(setq num2 (read))

(if (or (= num1 0) (= num2 0))
    (write-line "result is 0.0")
    (calculator num1 num2))

不要setq未声明的变量。使用let代替创建本地绑定。

(defun calculator (num1 num2)
  (write-line "In the function")
  (let ((res 0))
    (loop for lpr from 0 to 63
          do (progn
               (let ((end-bit (logand num2 1)))
                 (if (= end-bit 1)
                     (+ res num1))
                 (ash num2 -1)
                 (ash num1 1))))
    (write-line res)))

(let ((num1 (progn
              (write-line "Enter first number : ")
              (read)))
      (num2 (progn
              (write-line "Enter second number : ")
              (read))))
  (if (or (= num1 0) (= num2 0))
      (write-line "result is 0.0")
      (calculator num1 num2)))

progn中的:do之后您不需要loop

(defun calculator (num1 num2)
  (write-line "In the function")
  (let ((res 0))
    (loop for lpr from 0 to 63
          do (let ((end-bit (logand num2 1)))
               (if (= end-bit 1)
                   (+ res num1))
               (ash num2 -1)
               (ash num1 1)))
    (write-line res)))

(let ((num1 (progn
              (write-line "Enter first number : ")
              (read)))
      (num2 (progn
              (write-line "Enter second number : ")
              (read))))
  (if (or (= num1 0) (= num2 0))
      (write-line "result is 0.0")
      (calculator num1 num2)))

您的错误:错误明确告诉您“值0不是字符串类型”。它还告诉您在尝试调用write-line时遇到此错误。查看write-line的文档:它需要string作为第一个参数。你试图在某个地方给它一个0。唯一可能的地方是(write-line res),你总是有一个数字,而不是一个字符串。

您可以改用(format t "result is ~a.~%" res)

(defun calculator (num1 num2)
  (write-line "In the function")
  (let ((res 0))
    (loop for lpr from 0 to 63
          do (let ((end-bit (logand num2 1)))
               (if (= end-bit 1)
                   (+ res num1))
               (ash num2 -1)
               (ash num1 1)))
    (format t "result is ~a.~%" res)))

(let ((num1 (progn
              (write-line "Enter first number : ")
              (read)))
      (num2 (progn
              (write-line "Enter second number : ")
              (read))))
  (if (or (= num1 0) (= num2 0))
      (write-line "result is 0.0")
      (calculator num1 num2)))

在等待答案之前,您需要刷新输出以显示问题。

(defun calculator (num1 num2)
  (write-line "In the function")
  (let ((res 0))
    (loop for lpr from 0 to 63
          do (let ((end-bit (logand num2 1)))
               (if (= end-bit 1)
                   (+ res num1))
               (ash num2 -1)
               (ash num1 1)))
    (format t "result is ~a.~%" res)))

(let ((num1 (progn
              (write-line "Enter first number: ")
              (finish-output)
              (read)))
      (num2 (progn
              (write-line "Enter second number: ")
              (finish-output)
              (read))))
  (if (or (= num1 0) (= num2 0))
      (write-line "result is 0.0")
      (calculator num1 num2)))