没有包绑定的 Lisp 符号
我一直在做一些项目。它应该能够进行数值和符号计算。但现在我被一个问题困住了,我真的不知道如何解决。简而言之,假设我们在包中,
(in-package #:brand-new-package)
我们有符号数据库
(defvar var-symbol-database (make-hash-table :test #'equal))
读取和设置函数
(defun var-symbol (name)
(get-hash name var-symbol-database))
(defun set-var-symbol (name value)
(setf (get-hash name var-symbol-database) value))
(set-var-symbol 'temperature 300) ;K
(set-var-symbol 'f 200) ;Hz
(set-var-symbol 'k 1.3806504e-23) ;J K^-1
,现在在另一个文件(但相同的包)中,我将尝试评估这个方程,
(eval '(+ 2 (var-symbol 'f)))
它不会起作用。问题是,由于某些特定原因,哈希表中的键值是。
brand-new-package::f
我虽然我会像这样解决定义函数的问题
(set-var-symbol 1 '(var-symbol 'f)) ;Hz
,但它被解释为
(brand-new-package::var-symbol brand-new-package::f)
问题是程序可以创建许多不同的符号。它将计算电子电路方程。程序首先检查电容器、电阻器等设备对象。它由 MNA 创建电路表。
在此过程中,可以创建许多代表节点电压和电流的新符号,
(v1, v2, v3, i1, i2).
我需要某种方法来保存方程中出现的变量的计数和名称。因为它们将被传递给符号导数 ie (diff '(* (+ 40 v1) u2 ...) 'v1)) 我想到了一个想法,也许是错误的,让它们可以通过索引访问以将它们定义为
'(v 1) '(v 2) '(v 3).
列表使它们可评估,我添加到开始 var-variable funcall 中。所以列表变成了
'(var-variable v 1) '(var-variable v 2) '(var-variable v 3)
但正如我所写的,系统将其更改为
'(brand-new-package::var-variable brand-new-package::v 1) '(brand-new-package::var-variable brand-new-package::v 2) '(brand-new-package::var-variable brand-new-package::v 3)
如何允许用户通过键入 (var-symbol 'v 1) 来访问这些变量。我只能想象一种方式。使用字符串和导出函数(var-symbol)代替符号。然后就会这样工作
'(var-variable "v" 1)
,但有点令人困惑。
I've been working on some project. It should be able to do numerical and symbolic computing. But now I stuck on one problem and I don't really know how to resolve it. To be specific and short, let's say we are in package
(in-package #:brand-new-package)
Where we have symbol database
(defvar var-symbol-database (make-hash-table :test #'equal))
Reading and setting functions
(defun var-symbol (name)
(get-hash name var-symbol-database))
(defun set-var-symbol (name value)
(setf (get-hash name var-symbol-database) value))
(set-var-symbol 'temperature 300) ;K
(set-var-symbol 'f 200) ;Hz
(set-var-symbol 'k 1.3806504e-23) ;J K^-1
and now in another file (but same package) I will try to evaluate this equation
(eval '(+ 2 (var-symbol 'f)))
It won't work. Problem is that for some particular reason the value of key in hash table is.
brand-new-package::f
I though that I will solve the problem defining function like this
(set-var-symbol 1 '(var-symbol 'f)) ;Hz
But it is interpreted as
(brand-new-package::var-symbol brand-new-package::f)
The problem is that program can create many different symbols. It will compute electronic circuit equations. Program first inspect device objects like capacitors, resistors and so. It create circuit tablo by MNA.
During it many new symbols representing node voltages and currents could be created
(v1, v2, v3, i1, i2).
I needed some method to hold count and names of variables presented in equation. Because they will be passed to symbolic derivator ie (diff '(* (+ 40 v1) u2 ...) 'v1)) I came with an idea, maybe wrong, to make them reachable by index to define them as a list
'(v 1) '(v 2) '(v 3).
To make them evaluable I added to begining var-variable funcall. So list becomed
'(var-variable v 1) '(var-variable v 2) '(var-variable v 3)
But as I have written, system changes it to
'(brand-new-package::var-variable brand-new-package::v 1) '(brand-new-package::var-variable brand-new-package::v 2) '(brand-new-package::var-variable brand-new-package::v 3)
How to allow to users to acces these variables by typing (var-symbol 'v 1). I can imagine only one way. Instead of symbols use strings and export function (var-symbol). Then it will work this way
'(var-variable "v" 1)
But it is a little bit confusing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你正在复制 Lisp 已经做的事情。符号已经在表中进行管理,称为包。符号可以有一个值。装进包里就是INTERN。找到它是 FIND-SYMBOL 或者只是使用 Lisp READer。
如果您想要自己的符号索引表,哈希表就可以了。如果您不想处理这些符号的包,那么只需使用关键字符号即可。它们前面有一个冒号。 :温度就是一个例子。关键字符号自动包含在 KEYWORD 包中,并且它们会自行求值。
You are duplicating what Lisp already does. Symbols are already managed in tables, called packages. A symbol can have a value. Putting it into a package is INTERN. Finding it is FIND-SYMBOL or just using the Lisp READer.
If you want your own symbol index tables, hash tables are fine. If you don't want to deal with packages of those symbols, then just use keyword symbols. They have a single colon in front. :temperature would be an example. Keyword symbols are automagically in the package KEYWORD and they evaluate to themselves.
您所说的“问题”正如预期的那样。 Common Lisp 表示法
brand-new-package::var-symbol
表示符号var- symbol
位于包brand-new-package
中,该包是 lisp 读取该符号时的当前包。What you state to be a "problem" is as expected. The Common Lisp notation
brand-new-package::var-symbol
signifies that the symbolvar-symbol
is in the packagebrand-new-package
, which was the current package at the time the symbol was read by the lisp.