合并通用Lisp中的符号 [英] Merging symbols in common lisp

查看:133
本文介绍了合并通用Lisp中的符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在列表中插入一个字符.但是,我想将此字符与列表中的最后一个符号合并.使用追加和缺点,结果始终是两个不同的符号.好吧,我希望一个合并的符号成为我的结果.

I want to insert a char into a list. However, I want to merge this char with the last symbol in the list. With appends and cons the result is always two different symbols. Well, I want one merged symbol to be my result.

示例:

       (XXXX 'a '5) ====>  (a5)    

我想拥有的东西,而不是:

What I would like to have, instead of:

       (XXXX 'a '5) ====> (a 5)   

推荐答案

您不能在Lisp中合并符号".

You cannot "merge symbols" in Lisp.

首先,5不是符号,而是数字.如果您想要一个名为"5"的符号,则必须将其键入为|5|(例如).

First of all, 5 is not a symbol, but a number. If you want a symbol named "5" you have to type it as |5| (for example).

如果一个函数采用符号A和符号|5|,并产生符号A5,则该函数没有合并的符号.它创建了一个新符号,其名称是这些输入符号名称的串联.

If a function takes the symbol A and symbol |5|, and produces the symbol A5, it has not merged symbols. It has created a new symbol whose name is the catenation of the names of those input symbols.

设计正确的Lisp程序很少依赖于符号的命名方式.它们依赖于符号是唯一实体.

Properly designed Lisp programs rarely depend on how a symbol is named. They depend on symbols being unique entities.

如果您使用符号来标识事物,并且5A都标识了某个实体,则最佳答案不一定是创建一个新符号,至少在名称上是一个mashup这两个符号.例如,一个更好的设计可能是接受名称是多面的或以某种方式复合的.列表(A 5)可以用作名称.

If you're using symbols to identify things, and both 5 and A identify some entity, the best answer isn't necessarily to create a new symbol which is, in name at least, is a mashup of these two symbols. For instance, a better design might be to accept that names are multi-faceted or compound in some way. Perhaps the list (A 5) can serve as a name.

通用Lisp函数本身可以具有复合名称.例如,(setf foo)是一个函数名称.列表之类的聚集体可以是名称.

Common Lisp functions themselves can have compound names. For instance (setf foo) is a function name. Aggregates like lists can be names.

如果您只需要机器在运行时发明独特的符号,请考虑使用gensym功能.您可以将自己的前缀传递给它:

If you simply need the machine to invent unique symbols at run-time, consider using the gensym function. You can pass your own prefix to it:

(gensym "FOO") -> #:FOO0042

当然,前缀可以是通过symbol-name拔出的某些现有符号的名称.由于0042,符号#:FOO0042不是唯一的,而是因为它是地址空间中新分配的对象. #:表示未在任何程序包中进行检查.符号的名称为FOO0042.

Of course, the prefix can be the name of some existing symbol, pulled out via symbol-name. The symbol #:FOO0042 is not unique because of the 0042 but because it is a freshly allocated object in the address space. The #: means it is not interned in any package. The name of the symbol is FOO0042.

如果您仍然真的想要,获取一堆输入对象的打印表示形式并将其转换为符号的简单方法是:

If you still really want to, a simple way to take the printed representation of a bunch of input objects and turn it into a symbol is this:

(defun mashup-symbol (&rest objects)
  (intern (format nil "~{~a~}" objects)))

示例:

(mashup-symbol 1 2 3) -> |123|

(mashup-symbol '(a b) 'c 3) -> |(A B)C3|

这篇关于合并通用Lisp中的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆