Common Lisp中的未互连符号 [英] Uninterned symbols in Common Lisp

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

问题描述

几次我都遇到了未中断符号的概念,但是我对它们的含义并不完全清楚.

Several times I came across the notion of uninterned symbols, but I am not entirely clear about what they are.

是否有一种方法可以内生用(make-symbol)创建的符号?
我可以在不对符号进行赋值的情况下为其赋值吗?
是否可以重命名符号(interinter或uninterned)?
没有隔离符号的人还能做什么?

Is there a way to intern a symbol created with (make-symbol)?
Can I assign a value to a symbol without interning it?
Is it possible to rename a symbol (interned or uninterned)?
What else can one do with an uninterned symbol?

更新:
在这段代码中,符号发生了什么?

Update:
What is happening with symbols in this piece of code?

CL-USER> (defun func ()
           (let ((var 'sym))
             (print (find-symbol "sym"))
             (print var)))
FUNC
CL-USER> (func)

NIL 
SYM 
SYM

我不正确的理解是:
1. find-symbol输出nil,因此该符号没有被插入
2. var在不带#的情况下打印出sym:在开始时表示它已被禁闭

My incorrect understanding is:
1. find-symbol prints nil, so the symbol is not intered
2. var prints sym without #: in the beginning which means it is interned

推荐答案

未间断的符号通常用作名称或代号,以避免混乱的软件包或用于相关任务.

Uninterned symbols are mostly used as names or designators to avoid cluttering packages, or for related tasks.

例如:

T1> (find-symbol "T2")
NIL
NIL
T1> (find-symbol "T3")
NIL
NIL
T1> (defpackage t2)
#<Package "T2">
T1> (defpackage #:t3)
#<Package "T3">    
T1> (find-symbol "T2")
T2
:INTERNAL
T1> (find-symbol "T3")
NIL
NIL

如您所见,在第一个defpackage表单中使用t2可以在包t1中使用它,而在第二个defpackage中使用#:t3可以避免这种情况.之所以可行,是因为defpackage字符串指示符作为其第一个元素,并且不需要插入符号即可充当指示符.

As you can see, using t2 in the first defpackage form interns it in package t1, while using #:t3 in the second defpackage avoids this. This is possible, because defpackage takes a string designator as its first element, and a symbol doesn't need to be interned to function as a designator.

在这些情况和相关情况下,大多数情况下会故意使用未插入的符号.您还可以避免使用字符串或关键字来污染软件包,但在第一种情况下,使用默认的readtable情况的人可能会遇到 问题,而在第二种情况下,污染某些人关心的关键字包. (关于这是否真的是一件坏事,有不同的看法.)

These and related situations are where uninterned symbols are mostly used deliberately. You could also avoid polluting the package by using a string or a keyword, but in the first case, there may be problems with people using other than the default readtable-case, and in the second case you would pollute the keyword package, which some people care about. (There are different opinions as to whether this really is such a bad thing or not.)

然后,在某些情况下,符号会丢失其主包装(例如,通过unintern ing),并且至少变得显然没有被隔离. (它可能仍然被装在另一个包装中.)

Then, there are situations where a symbol loses its home-package (by uninterning, for example) and becomes at least apparently uninterned. (It may still be interned in another package.)

T1> (defparameter *t2* (find-symbol "T2"))
*T2*
T1> (import *t2* "T3")
T
T1> (symbol-package *t2*)
#<Package "T1">
T1> (unintern *t2*)
T
T1> (find-symbol "T2")
NIL
NIL
T1> (symbol-package *t2*)
NIL
T1> *t2*
#:T2
T1> (find-symbol "T2" "T3")
#:T2
:INTERNAL
T1> (unintern *t2* "T3")
T
T1> (import *t2* "T3")
T
T1> *t2*
T3::T2
T1> (symbol-package *t2*)
#<Package "T3">

所以,答案

有没有办法使用(make-symbol)创建的符号?

Is there a way to intern a symbol created with (make-symbol)?

是:

T1> (import (make-symbol "T4"))
T
T1> (find-symbol "T4")
T4
:INTERNAL

我可以不对符号赋值而给它赋值吗?

Can I assign a value to a symbol without interning it?

是的,虽然您丢失了可以通过名称和包装唯一标识的属性,但仍可以使用其值槽,plist等.

Yes, while you're losing the property that it can be uniquely identified by its name and packagage, you can still use its value slot, plist, etc.:

T1> (let ((symbol '#:t5))
      (setf (symbol-value symbol) 1)
      (setf (get symbol :foo) :bar)
      (setf (symbol-function symbol) (lambda ()))
      (values (symbol-value symbol)
              (get symbol :foo)
              (symbol-function symbol)))
1
:BAR
#<Anonymous Function #xC829036>

是否可以重命名符号(interinter或uninterned)?

Is it possible to rename a symbol (interned or uninterned)?

修改符号名称的后果是不确定的.

The consequences of modifying a symbol's name are undefined.

一个未隔离的符号还能做什么?

What else can one do with an uninterned symbol?

我真的认为它们在包定义中通常被用作指示符,但通常的回答是:在您不想使用硬编码字符串来命名事物并且不想污染任何包的情况下,它们会很有用.

I really think they're mostly used as designators in package definitions, but to the general answer would be: They can be useful in situations where you want to name things without using hardcoded strings and don't want to pollute any package.

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

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