首页 > 解决方案 > lisp 将值传递给函数?

问题描述

我是 lisp 的新手,在我的代码中,我想将 3 个第一位转换为整数,并检查下一位中的个数是否等于该值,这是我的代码:

(defun new (vi n res)
    (cond ((= vi nil) (cond ((= 0 res) t) ))
          ((= 2 n) (new (cdr vi) 1 (* (car vi) 4))) 
          ((= 1 n) (new (cdr vi) 0 (+ (res) (* (car vi) 2))))  
          ((= 0 n) (new (cdr vi) -1 (+ (res) (car vi ))))
          ((= vi nil) (nil))
          ((= -1 n) (new (cdr vi) -1 (- res (car vi))))))

我想测试它,但是当我写它时:

(new ( 0 1 0 0 0 0 1 0 0 1 0) 2 0 )

它错误 0 不是我尝试过的功能:

 (new (list `0 `1 `0 `0 `0 `0 `1 `0 `0 `1 `0) 2 0 )

但它错误

标签: lisp

解决方案


SBCL 在编译您的函数时显示以下警告:

; in: DEFUN NEW
;     (= VI NIL)
; 
; caught WARNING:
;   Constant NIL conflicts with its asserted type NUMBER.
;   See also:
;     The SBCL Manual, Node "Handling of Types"
; in: DEFUN NEW
;     (NIL)
; 
; caught WARNING:
;   The function NIL is undefined, and its name is reserved by ANSI CL so that even
;   if it were defined later, the code doing so would not be portable.




;     (RES)
; 
; caught STYLE-WARNING:
;   undefined function: COMMON-LISP-USER::RES
; 
; compilation unit finished
;   Undefined functions:
;     NIL RES
;   caught 2 WARNING conditions
;   caught 1 STYLE-WARNING condition

推荐阅读