Lisp/scheme中的符号到底是什么? [英] What exactly is a symbol in lisp/scheme?

查看:188
本文介绍了Lisp/scheme中的符号到底是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于对全能者的热爱,我尚未了解符号'iamasymbol的目的.我了解数字,布尔值,字符串...变量.但是符号对于我的小命令性思维来说实在太多了.我究竟将它们用于什么用途?应该如何在程序中使用它们?我对这个概念的理解只是失败.

For the love of the almighty I have yet to understand the purpose of the symbol 'iamasymbol. I understand numbers, booleans, strings... variables. But symbols are just too much for my little imperative-thinking mind to take. What exactly do I use them for? How are they supposed to be used in a program? My grasp of this concept is just fail.

推荐答案

在Scheme和Racket中,符号就像不可变的字符串,恰好被插入,因此可以将符号与eq?进行比较(快速,本质上是指针比较) ).符号和字符串是单独的数据类型.

In Scheme and Racket, a symbol is like an immutable string that happens to be interned so that symbols can be compared with eq? (fast, essentially pointer comparison). Symbols and strings are separate data types.

符号的一种用法是轻量级枚举.例如,可能会说一个方向是'north'south'east'west.您当然可以将字符串用于相同的目的,但是效率会稍低.使用数字将不是一个好主意.以尽可能明显和透明的方式表示信息.

One use for symbols is lightweight enumerations. For example, one might say a direction is either 'north, 'south, 'east, or 'west. You could of course use strings for the same purpose, but it would be slightly less efficient. Using numbers would be a bad idea; represent information in as obvious and transparent a manner as possible.

对于另一个示例,SXML是使用列表,符号和字符串的XML表示.特别是,字符串代表字符数据,符号代表元素名称.因此,XML <em>hello world</em>将由值(list 'em "hello world")表示,该值可以更紧凑地编写为'(em "hello world").

For another example, SXML is a representation of XML using lists, symbols, and strings. In particular, strings represent character data and symbols represent element names. Thus the XML <em>hello world</em> would be represented by the value (list 'em "hello world"), which can be more compactly written '(em "hello world").

符号的另一用途是用作键.例如,您可以将方法表实现为字典,将符号映射到实现功能.要调用方法,您需要查找与方法名称相对应的符号. Lisp/Scheme/Racket使真的变得容易,因为该语言已经在标识符(语言语法的一部分)和符号(语言中的值)之间建立了内置的对应关系.这种对应关系使支持 macros 变得容易,该宏实现了该语言的用户定义语法扩展.例如,可以使用方法名称"(由类系统定义的语法概念)和符号之间的隐式对应关系,将类系统实现为宏库.

Another use for symbols is as keys. For example, you could implement a method table as a dictionary mapping symbols to implementation functions. To call a method, you look up the symbol that corresponds to the method name. Lisp/Scheme/Racket makes that really easy, because the language already has a built-in correspondence between identifiers (part of the language's syntax) and symbols (values in the language). That correspondence makes it easy to support macros, which implement user-defined syntactic extensions to the language. For example, one could implement a class system as a macro library, using the implicit correspondence between "method names" (a syntactic notion defined by the class system) and symbols:

(send obj meth arg1 arg2)
=>
(apply (lookup-method obj 'meth) obj (list arg1 arg2))

(在其他Lisps中,我所说的大部分都是荒谬的,但是还有其他一些事情需要了解,例如IIRC的包和函数与可变插槽).

(In other Lisps, what I've said is mostly truish, but there are additional things to know about, like packages and function vs variable slots, IIRC.)

这篇关于Lisp/scheme中的符号到底是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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