谐音水平 [英] Levels of Homoiconicity

查看:190
本文介绍了谐音水平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的的后续操作上一个问题.我不认为Lisp代码与Von Neumann架构上的机器代码具有同质性.在我看来,在两种情况下,代码都表示为数据,但显然,与Lisp相比,您可以在机器代码中更自由地利用此属性.

This is a follow up to my previous question. I’m not convinced that Lisp code is as Homoiconic as machine code on a Von Neumann architecture. It seems obvious to me that in both cases code is represented as data, but it also seems apparent that you can exploit this property much more freely in machine code than you can in Lisp.

当随意修改机器代码时,自我修改代码是如此容易,它总是在无时无刻不在发生,而且(以我的经验)常常会产生搞笑的结果.在编写一个简单的打印数字0-15"程序时,我的一个指针可能会出现"off by one"错误.我最终会不小心将寄存器1中的任何内容转储到包含下一条指令的内存中的地址中,而是执行了一条随机指令. (当它成为某种"goto"时总是很棒的.上帝知道它会在哪里结束以及在那之后发生的事情)

When mucking around with machine code, self modifying code is so easy it happens all the time, often by accident and with (in my experience) hilarious results. While writing a simple "print the numbers 0-15" program I might have an "off by one" error with one of my pointers. I’ll end up accidentally dumping whatever is in Register 1 into the address in memory that contains the next instruction, and a random instruction gets executed instead. (Always great when it’s some sort of "goto". God knows where it’s going to end up and what it’s going to do after that happens)

代码和数据之间确实没有分隔.一切都同时是一条指令(即使只是一个NOP),一个指针和一个普通的旧数字.而且代码可能会在您眼前更改.

There really is no separation between code and data. Everything is simultaneously an instruction (even if it’s just a NOP), a pointer, and a plain old number. And it’s possible for the code to change before your eyes.

请帮我解决Lisp问题,我一直在抓狂.说我有以下程序:

Please help me with a Lisp scenario I’ve been scratching my head over. Say I’ve got the following program:

(defun factorial (n)
   (if (<= n 1)
       1
       (* n (factorial (- n 1)))))
; -- Demonstrate the output of factorial --
; -- The part that does the Self modifying goes here –
; -- Demonstrate the changed output of factorial

现在我要发生的是将一些Lisp代码附加到该程序中,该Lisp代码会将*更改为+,将< =更改为> =,并在其中的某个位置粘贴(+ 1 2 3),然后通常会增加功能.然后,我希望程序执行产生的绝对混乱.

Now what I want to happen is to append to this program some Lisp code which will change the * to a +, change the <= to a >=, stick a (+ 1 2 3) somewhere in there, and generally bugger the function up. And then I want the program to execute the absolute mess that results.

要点:除非我在示例代码中犯了致命错误,否则您只能更改-– More code goes here –-部分.您在上方看到的是代码.我不希望您引用整个列表并将其存储在变量中,以便可以将其作为具有相同名称的单独函数进行操作和吐出;我不希望对阶乘进行标准重新定义,因为它完全不同.我想要那个代码,就在那儿,我可以在屏幕上看到它像机器代码一样在我眼前改变.

Key point: Unless I’ve made some fatal error in the sample code you’re only allowed to alter the -– More code goes here –- part. What you see above is the code. I don’t want you quoting the entire list and storing it in a variable so that it can be manipulated and spat out as a separate function with the same name; I don’t want a standard redefinition of factorial as something completely different. I want that code, right there that I can see on my screen to change itself before my eyes, just like machine code.

如果这是一个不可能/不合理的要求,那么在我看来,它只会进一步巩固以下观念:同声性不是某种语言具有或不具有的离散属性,它是频谱,Lisp不在乎出血边缘. (或者,Lisp与它们一样是同质的,我正在寻找其他术语来描述机器代码式的自我修改)

If this is an impossible/unreasonable request then it only solidifies further in my mind the idea that Homoiconicity is not a discrete property that a language either has or doesn’t have, it is a spectrum and Lisp isn't at the bleeding edge. (Alternatively Lisp is as Homoiconic as they come and I'm looking for some other term to describe machine-code-esque self-modification)

推荐答案

那很容易.您只需要更改列表表示形式.您只需要一个Lisp 解释器.

That's easy. You only need to change the list representation. All you need is a Lisp interpreter.

Common Lisp实现 LispWorks 为我们提供了Lisp解释器:

The Common Lisp implementation LispWorks provides us with a Lisp interpreter:

CL-USER 137 > (defun factorial (n)
                (if (<= n 1)
                    1
                  (* n (factorial (- n 1)))))
FACTORIAL

CL-USER 138 > (fifth (function-lambda-expression #'factorial))
(IF (<= N 1) 1 (* N (FACTORIAL (- N 1))))

CL-USER 139 > (fourth (fifth (function-lambda-expression #'factorial)))
(* N (FACTORIAL (- N 1)))

CL-USER 140 > (setf (first (fourth (fifth (function-lambda-expression
                                             #'factorial))))
                    '+)
+

CL-USER 141 > (fourth (fifth (function-lambda-expression #'factorial)))
(+ N (FACTORIAL (- N 1)))

CL-USER 142 > (factorial 10)
55

CL-USER 143 > (setf (first (fourth (fifth (function-lambda-expression
                                             #'factorial))))
                    '*)
*

CL-USER 144 > (factorial 10)
3628800

这是一个函数修改自身的示例.为了使操作更简单,我使用了Common Lisp的功能:它使我可以编写代码,它不仅是一些嵌套列表,而且是图形.在这种情况下,该函数可以访问自己的代码:

Here is an example where a function modifies itself. To make it slightly easier, I use a feature of Common Lisp: it allows me to write code which is not just some nested list, but a graph. In this case the function can access its own code:

CL-USER 180 > (defun factorial (n)
                (if (<= n 1)
                    1
                  (progn 
                    (setf (first '#1=(* n (factorial (- n 1))))
                          (case (first '#1#)
                            (+ '*)
                            (* '+)))
                    #1#)))
FACTORIAL

以上功能可以通过修改其代码来使用+*.

Above function alternatively uses + or * by modifying its code.

#1=是表达式中的标签,#1#然后引用该标签.

#1= is a label in the expression, #1# then references that label.

CL-USER 181 > (factorial 10)
4555

在早期(70年代/80年代)某些Lisp组中,开发人员不是使用文本编辑器来编写Lisp代码,而是使用结构编辑器.编辑器命令直接更改了Lisp代码的结构.

In earlier times (70s/80s) in some Lisp groups the developers were not using a text editor to write Lisp code, but a structure editor. The editor commands were directly changing the structure of the Lisp code.

这篇关于谐音水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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