有没有办法在 Common-Lisp 中不传递参数而不是传递“NIL”?

Gap*_*512 0 lisp common-lisp

我根据用户输入调用函数,但有些函数有两个参数,有些只有一个参数。有没有一种方法可以在参数的值为“NIL”时不传递参数,而不是在每个函数上使用&可选参数(并且从不使用它)?

这是一个交互式小说游戏,其中用户键入一些命令,这些命令将转换为函数调用。

(defun inputs (state)
    (format *query-io* "> ")
    (force-output *query-io*)
    (let* ((entry (cl-ppcre:split "\\s+" (string-downcase (read-line *query-io*))))
      (function (car entry))
      (args (cdr entry)))
      (if (valid-call function)
      (funcall (symbol-function (read-from-string function))
           state
           args)
      (progn
        (format *query-io* "Sorry, I don't know the command '~a'~%~%" function)
        (inputs state)))))
Run Code Online (Sandbox Code Playgroud)

如果用户输入是“装备剑”,我需要调用函数“装备”并传递 '(“剑”) 作为参数,但如果用户输入是“状态”,我需要调用函数“状态”而不需要传递“args”,而不是将它们作为“NIL”传递

sds*_*sds 5

我认为你想使用apply 而不是funcallfind-symbol而不是 (出于安全原因read-from-string这实际上很重要!)并且而 不是:destructuring-bindlet*

(defun inputs (state)
  (format *query-io* "> ")
  (force-output *query-io*)
  (destructuring-bind (command &rest args)
      (cl-ppcre:split "\\s+" (string-downcase (read-line *query-io*)))
    (if (valid-call command)
        (apply (find-symbol command) state args)
        (progn
          (format *query-io* "Sorry, I don't know the command '~a'~%~%" command)
          (inputs state)))))
Run Code Online (Sandbox Code Playgroud)

使用apply可让您的命令接受任意数量的参数而不是一个。

事实上,您valid-call可能应该返回要调用的函数:

(let ((f (valid-call function)))
  (if f
      (apply f state args)
      ...)
Run Code Online (Sandbox Code Playgroud)