指定者的性质是什么? [英] What is the nature of designators?

查看:76
本文介绍了指定者的性质是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Svante只是通过在另一个答案中显示字符串指示符来震撼我的头脑:

 (string =:&& ;)-> T 

看CLHS,他们说一个指示符是一个表示另一个的对象很好,但是由于这些是不同的对象,因此某些地方需要进行某种强制。我的意思是,如果以下列表指示符可以由非零原子满足,则存在某些逻辑可以处理此问题。


清单指示者对象列表的指示符;也就是说,一个
对象表示一个列表,并且是以下之一:非零原子
(表示元素为该非零原子的单例列表)或
适当列表(表示自己)。


我认为指示符可能是一个概念,例如,泛型函数。来自CLHS ...


除非另有说明,否则在指定对象
可能多次使用的情况下,是取决于实现方式,是仅强制对象一次
强制转换,还是强制对象每次使用
时强制转换。


...使得看上去非常具体。



所以我的问题




  • 在实现中如何实现指示符的示例是什么?

  • 该机制是否可以由用户以任何方式扩展?

  • 这种机制在指定人之间是否一致? (从clhs看,似乎有18种指示符)



干杯

解决方案

一个指示符仅是(或少于)一个指示的对象。关于它们的语言没有什么特别的;名称符的概念只是使某些编程实践更容易的一种。词汇表说:


指示符。表示另一个对象的对象。在操作符的
字典条目中,如果参数被描述为某个类型的
指示符,则该操作符的描述以
的方式编写,并假定对该类型具有适当的强制性已经发生了
;也就是说,该参数已经是指定类型。
有关更多详细信息,请参见第1.4.1.5节(标识符)


该部分的链接很有帮助:


1.4.1.5标识符



指示符是表示另一个对象的对象。



在将运算符的参数描述为指示符的情况下,
运算符的描述采用以下方式编写:假定参数的
值是表示的对象;也就是说,
参数已经是指定类型。 (
对象的特定性质由 latype标记或
type标记表示,可以在术语表 la中找到;类型)。


能够在词汇表中查找内容会有所帮助。例如,字符串指示符可以代表字符串:


字符串指示符。字符串的标识符;也就是说,一个
对象表示一个字符串,并且是以下之一:一个字符(表示
a单例字符串,该字符为唯一元素),一个
符号(表示该字符串是其名称)或字符串(表示
本身)。目的是使该术语与字符串的行为
一致;扩展字符串的实现必须以兼容的方式扩展该术语的含义


该标准也恰好定义了 string 函数,该函数获取由指定的字符串字符串指示符:


返回由x描述的字符串;具体来说:




  • 如果x是字符串,则返回它。

  • 如果x是符号,则返回其名称。

  • 如果x是一个字符,则返回包含该字符的字符串。字符串可能会执行其他由实现定义的
    转换。


这简化了必须使用字符串和类似字符串的函数。例如,您可以定义 make-person 函数,该函数带有字符串指示符:

 (defun make -person(名称)
返回一个由NAME指定的名称的人。
(列表:名称(字符串名称)))

(取消命名人的名称(人)
返回一个人的名字(字符串)。
(getf人:名称))

指定符的概念不过是使定义灵活的API更加容易的编程约定。 Common Lisp被定义为一种统一一堆现有Lisps的语言,它可能是统一不同实现的行为的更简便方法之一。



case


列表标记。对象列表的指示符;也就是说,
是表示列表的对象,并且是以下之一:非零原子
(表示元素为该非零原子的单例列表)或
适当列表(表示自己)。



案例 键表{normal-clause} * [否则子句] =>结果*



正常子句:: =(键形式*)



键-对象列表的指示符。在这种情况下,
是符号t,否则可能不会用作键指示符。为了使
自己将这些符号称为键,必须分别使用指示符(t)和
(否则)。


我不知道返回列表指定者指定的列表的函数,但是编写起来很容易(这不能处理 t 的特殊行为以及大小写所需要的其他,但是它通常处理列表指示符):

$ c>(defun to-list(x)
返回由x指定的列表。
(if(listp x)x
(list x)))

有时这样的约定在您自己的代码中可能会很有用,尤其是当您在注册表东西。例如,如果您编写了以下任何一个文件:

 (defmacro deftransform(name& rest args)
`(setf (gethash',name * transforms *)
(make-transform,@ args)))

 (defmacro deftransform(name& rest args)
(setf(get',name'transform) (make-transform,@ args)))

然后可以将转换指示符的概念定义为转换对象或符号(在* transforms *表中指定该符号的值,或该符号上的transform属性的值)。例如:

 (defun transform(x)
(if(transformp x)x
(gethash name) ****)))

 (defun transform(x)
(if(transformp x)x
(get x'transform)))

这可能会使部分代码更易于使用。功能指示符相似


Svante just blew my mind by showing string designators in another answer do this:

(string= :& "&") -> T

Looking at CLHS, they say A designator is an object that denotes another object. which is fine but as these are different objects some kind of coercion needs to happen somewhere. By which I mean if the following list designator can be satisfied by a 'non-nil atom' some logic exists somewhere for handling this.

list designator n. a designator for a list of objects; that is, an object that denotes a list and that is one of: a non-nil atom (denoting a singleton list whose element is that non-nil atom) or a proper list (denoting itself).

I thought designators just could be a concept resulting from, for example, generic functions.. but the following line from CLHS...

Except as otherwise noted, in a situation where the denoted object might be used multiple times, it is implementation-dependent whether the object is coerced only once or whether the coercion occurs each time the object must be used.

... makes then seem very concrete.

So my questions

  • What is an example of how designators could be implemented in an implementation?
  • Is this mechanism extensible in any way by users?
  • Is this mechanism consistent across designators? (looking in clhs it seems there are 18 kinds of designator)

Cheers

解决方案

A designator is nothing more (or less) than an object that designates another. There's nothing special in the language about them; the concept of designators is just one that makes certain programming practices easier. The glossary says:

designator n. an object that denotes another object. In the dictionary entry for an operator if a parameter is described as a designator for a type, the description of the operator is written in a way that assumes that appropriate coercion to that type has already occurred; that is, that the parameter is already of the denoted type. For more detailed information, see Section 1.4.1.5 (Designators).

The link to that section is helpful:

1.4.1.5 Designators

A designator is an object that denotes another object.

Where a parameter of an operator is described as a designator, the description of the operator is written in a way that assumes that the value of the parameter is the denoted object; that is, that the parameter is already of the denoted type. (The specific nature of the object denoted by a “«type» designator” or a “designator for a «type»” can be found in the Glossary entry for “«type» designator.”)

Being able to look for things in the glossary helps. For instance, a string designator is something that can stand for a string:

string designator n. a designator for a string; that is, an object that denotes a string and that is one of: a character (denoting a singleton string that has the character as its only element), a symbol (denoting the string that is its name), or a string (denoting itself). The intent is that this term be consistent with the behavior of string; implementations that extend string must extend the meaning of this term in a compatible way.

The standard also happens to define the function string that gets the string designated by a string designator:

Returns a string described by x; specifically:

  • If x is a string, it is returned.
  • If x is a symbol, its name is returned.
  • If x is a character, then a string containing that one character is returned. string might perform additional, implementation-defined conversions.

This simplifies the implementation of functions that have to work with strings and string like things. For instance, you can define a make-person function takes a string designator:

(defun make-person (name)
  "Return a person with the name designated by NAME."
  (list :name (string name)))

(defun person-name (person)
  "Return the name of a person (a string)."
  (getf person :name))

The concept of designator isn't anything but a programming convention that makes defining flexible APIs easier. Common Lisp was defined as a language to unite a bunch of existing Lisps, and it may have been one of the easier ways to unify the behavior of different implementations.

There's a concept of list designator that gets used in case

list designator n. a designator for a list of objects; that is, an object that denotes a list and that is one of: a non-nil atom (denoting a singleton list whose element is that non-nil atom) or a proper list (denoting itself).

case keyform {normal-clause}* [otherwise-clause] => result*

normal-clause::= (keys form*)

keys—a designator for a list of objects. In the case of case, the symbols t and otherwise may not be used as the keys designator. To refer to these symbols by themselves as keys, the designators (t) and (otherwise), respectively, must be used instead.

I don't know of a function that returns the list designated by a list designator, but it's easy enough to write (this doesn't handle the special behavior of t and otherwise that case needs, but it handles list designators in general):

(defun to-list (x)
  "Return the list designated by x."
  (if (listp x) x
    (list x)))

Conventions like these can be useful in your own code sometimes, especially if you're defining things where there's a "registry" of things. E.g., if you have written either of:

(defmacro deftransform (name &rest args)
  `(setf (gethash ',name *transforms*)
         (make-transform ,@args)))

(defmacro deftransform (name &rest args)
  (setf (get ',name 'transform) (make-transform ,@args)))

Then you can define the concept of a transform designator as either a transform object, or a symbol (which designates the value for the symbol in the *transforms* table, or the value of transform property on the symbol). E.g.:

(defun transform (x)
  (if (transformp x) x
    (gethash name *transforms*)))

(defun transform (x)
  (if (transformp x) x
    (get x 'transform)))

That might make parts of your code easier to use. Function designators are similar

这篇关于指定者的性质是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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