common-lisp - 指示符的性质是什么?

标签 common-lisp

Svante 通过在另一个答案中显示字符串指示符让我大吃一惊:

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

看看CLHS,他们说A designator is an object that denotes another object.这很好,但由于这些是不同的对象,因此需要在某处进行某种强制。我的意思是,如果“非零原子”可以满足以下列表指示符,则某些逻辑存在用于处理此问题。

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).



我认为指示符可能是一个由泛型函数产生的概念......但是下面来自 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.



...使然后看起来非常具体。

所以我的问题
  • 什么是指示符如何在实现中实现的示例?
  • 这种机制是否可以由用户以任何方式扩展?
  • 这种机制在指示符之间是否一致? (在clhs里看好像有18种指示符)

  • 干杯

    最佳答案

    指示符只不过是(或更少)指代另一个对象的对象。他们的语言没有什么特别之处。指示符的概念只是使某些编程实践更容易的一种。词汇表说:

    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).



    该部分的链接很有帮助:

    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.”)



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

    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.



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

    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.


    这简化了必须处理字符串和类似字符串的函数的实现。例如,您可以定义一个 制作人函数采用字符串指示符:
    (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))
    

    指示符的概念不过是一种编程约定,它使定义灵活的 API 变得更容易。 Common Lisp 被定义为一种将一堆现有 Lisp 联合起来的语言,它可能是统一不同实现行为的更简单的方法之一。

    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.



    我不知道返回由列表指示符指定的列表的函数,但它很容易编写(这不处理 t 的特殊行为,否则 cas​​e 需要,但它一般处理列表指示符):
    (defun to-list (x)
      "Return the list designated by 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* 表中符号的值,或符号上变换属性的值)。例如。:
    (defun transform (x)
      (if (transformp x) x
        (gethash name *transforms*)))
    
    (defun transform (x)
      (if (transformp x) x
        (get x 'transform)))
    

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

    关于common-lisp - 指示符的性质是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27427925/

    相关文章:

    lisp - 口齿不清错误 : Expected-type: REAL datum: NIL

    方案或 Common Lisp

    recursion - 普通口齿不清 : error "CDR LST should be a lambda expression"

    clojure - cl-format 重用之前的参数

    lisp - 这个常见的 lisp 代码有什么问题?

    lisp - 不使用 nthcdr 获取列表的第 n 个 cdr

    lisp - 我如何让 Ltk 显示用户正在写的内容以及函数打印的内容?

    lisp - 包的文件在哪里?

    lisp - 如何告诉 lisp 阅读器函数在解析过程中忽略错误

    Lisp IO 问题