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

问题描述

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

 (string =:&& ;)-> T 

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

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

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



所以我的问题




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

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

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



干杯

解决方案

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

该部分的链接很有帮助:

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

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

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

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

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

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



case

我不知道返回列表指定者指定的列表的函数,但是编写起来很容易(这不能处理 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.

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

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

The link to that section is helpful:

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

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

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

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

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

10-19 20:51