每日概念:同调性

photo by sum+it from Pexels

The word homoiconic can be understood by inspecting its roots: homo, meaning "the same" and icon, meaning "representation". According to Wikipedia, a language is homoiconic "if a program written in it can be manipulated as data using the language, and thus the program's internal representation can be inferred just by reading the program itself."

但是这个定义可能会造成混淆。 大多数语言都有eval()函数或类似功能,可以读取文本(可能从文件中读取)并像执行源代码一样执行该文本。 这是否意味着任何带有eval()类功能是谐音的吗? 不完全的。

一世n layman's terms, a programming language is homoiconic if its internal and external representations are the same. In a perfectly homoiconic language, source code can be run, immediately, without any interpretation. Because homoiconicity precludes the need for any interpretation -- if the external representation matches the program's internal representation, what is there to interpret?

这个严格的定义实质上排除了任何编程语言的同源性,因为即使二进制代码的文本表示形式仍然与二进制代码本身不同。

Accordingly, even languages like LISP, which purport to be homoiconic, are more correctly described as paraiconic, a term that the link above proposes. In these paraiconic languages, the source code of any program is itself the source code for a particular data structure within that language, which can be eval()-uated and manipulated as an object within that language. This is where the common definition of homoiconicity, "code as data", comes from.

可以想象这是一种完全用JSON(JavaScript对象表示法)编写的理论语言:

{
  "main" : {
    "type"   : "function",
    "params" : [ "args" : [...] ],
    "body"   : [ ... ],
    ...
  }
}

如果该语言还具有处理JSON文件的功能,则它可以对自己的源代码进行任意更改。 这就是为什么“将代码作为数据”的想法有意义的原因。 另一个(也许稍微简单些)的示例是LISP,其中S表达式可以描述整个程序,但它本身是LISP编程语言中的对象:

(1 2 3)

上面的LISP代码-一个S表达式-创建三个元素的列表1个,2,3。 该对象的字符串表示形式-(1个 2 3)-是恰好 equal to the code needed to create the object itself. Since LISP can manipulate S expressions,and since all LISP programs are written entirely in S expressions,every LISP program,no matter how complex,is simply a LISP list. Every LISP program is an object which can be manipulated using the LISP language.

This is not the same as the internal representation of the program (the abstract syntax tree, AST), despite some claims online.

这是不对对于Java之类的语言,对象的字符串表示形式通常不同于创建该对象所需的源代码:

jshell> int arr[] = { 1, 2, 3 };
arr ==> int[3] { 1, 2, 3 }

jshell> arr.toString()
$3 ==> "[I@1bce4f0a"

In order to recreate this in a language like Java, for instance, we would need to be able to recover, via reflection or otherwise, the actual name of the variable arr. This is not currently possible in Java. Additionally, we would need to be able to recover the type of the arr object, which -- thanks to type erasure -- is also unavailable. All Java objects are of class Object at runtime.

讽刺性意味着LISP程序可以非常容易地评估,解释和修改其他LISP程序。 由于可以从其字符串表示形式来解释格式正确的S表达式,并且由于所有LISP程序都是简单的S表达式,因此LISP可以轻松地从外部文件读取LISP程序并将其作为对象进行操作。 确实是“代码即数据”。


我希望这种解释可以使我们对两者之间的区别有所了解谐音而且更常见讽刺性,以及某些语言如何启用此属性,而其他语言则使它变得困难或不可能。

from: https://dev.to//awwsmm/concept-of-the-day-homoiconicity-4did

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值