小编Gap*_*512的帖子

包通信问题Common-Lisp

我定义了两个包:(game文件game.lisp)和commands(文件commands.lisp),由game.asd文件加载.我在调用命令函数(已导出)时遇到了麻烦,使用(symbol-function (find-symbol "test-function" 'commands)),返回函数未定义,即使(find-symbol "test-function" 'commands)返回函数是外部函数,也属于commands包.

game.asd文件的代码是:

(asdf:defsystem "game"
  :depends-on (#:cl-ppcre)
  :components ((:file "game")
               (:file "commands")))
Run Code Online (Sandbox Code Playgroud)

game.lisp与启动:

(defpackage :game
  (:use :cl :cl-ppcre))
Run Code Online (Sandbox Code Playgroud)

commands.lisp与启动:

(defpackage :commands
  (:use :cl)
  (:export "test-function"))
Run Code Online (Sandbox Code Playgroud)

我需要使用这个in-package功能吗?从game.lisp我调用存储在commands.lisp文件中的命令,其中一些调用一些函数game.lisp,例如:

(defun test-function ()
  (progn
    (format *query-io* "Worked!~%")
    (start)))
Run Code Online (Sandbox Code Playgroud)

test-function位于上的命令包,但调用start功能,属于game.lisp.

我打电话时,我希望调用该test-function函数(symbol-function (find-symbol …

lisp common-lisp

2
推荐指数
1
解决办法
92
查看次数

如何从对象引用中读取插槽而不评估指针

我想从另一个对象内部的指针获取属性的值,但是在不评估引用的情况下访问它会使我出错

When attempting to read the slot's value (slot-value), the slot
POS is missing from the object *NODE-1*.
Run Code Online (Sandbox Code Playgroud)

这是一段模拟错误的代码:

(defclass node ()
  ((pos 
    :initarg :pos
    :initform '(0 0)
    :accessor pos)))

(defclass edge ()
  ((vertices
    :initarg :vertices
    :accessor vertices)))

(defparameter *node-1* (make-instance 'node))
(defparameter *node-2* (make-instance 'node :pos '(100 100)))
(defparameter *edge-1* (make-instance 'edge :vertices '(*node-1* *node-2*)))
Run Code Online (Sandbox Code Playgroud)

之后,评估此表达式将引发错误

(slot-value (car (slot-value *edge-1* 'vertices)) 'pos)
Run Code Online (Sandbox Code Playgroud)

但是这个有预期的行为

(slot-value (eval (car (slot-value *edge-1* 'vertices))) 'pos)
Run Code Online (Sandbox Code Playgroud)

我已经知道它eval可用于丑陋的骇客,这就是为什么我试图找到一种巧妙的方式来做我需要的事情的原因。

lisp evaluation symbols list common-lisp

2
推荐指数
1
解决办法
61
查看次数

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

我根据用户输入调用函数,但有些函数有两个参数,有些只有一个参数。有没有一种方法可以在参数的值为“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”传递

lisp common-lisp

0
推荐指数
1
解决办法
407
查看次数

标签 统计

common-lisp ×3

lisp ×3

evaluation ×1

list ×1

symbols ×1