“数据只是愚蠢的代码,而代码只是智能数据"是什么意思? [英] What does “Data is just dumb code, and code is just smart data” mean?

查看:21
本文介绍了“数据只是愚蠢的代码,而代码只是智能数据"是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在 The Structure and Interpretation of计算机程序:

数据只是愚蠢的代码,而代码只是智能数据

Data is just dumb code, and code is just smart data

我不明白这是什么意思.有人可以帮助我更好地理解它吗?

I fail to understand what it means. Can some one help me to understand it better?

推荐答案

这是 SICP 的基本课程之一,也是计算机科学中最强大的思想之一.它是这样工作的:

This is one of the fundamental lessons of SICP and one of the most powerful ideas of computer science. It works like this:

我们认为的代码"实际上没有能力自己做任何事情.代码仅在解释上下文中定义程序——在该上下文之外,它只是一个字符流.(实际上是比特流,实际上是电脉冲流.但让我们保持简单.)代码的含义由您运行它的系统定义——而这个系统只是将您的代码视为告诉它您想要做什么的数据.C 源代码由 C 编译器解释为描述您希望它创建的目标文件的数据.加载器将目标文件视为描述要排队执行的一些机器指令的数据.机器指令被 CPU 解释为定义它应该经历的状态转换序列的数据.

What we think of as "code" doesn't actually have the power to do anything by itself. Code defines a program only within a context of interpretation -- outside of that context, it is just a stream of characters. (Really a stream of bits, which is really a stream of electrical impulses. But let's keep it simple.) The meaning of code is defined by the system within which you run it -- and this system just treats your code as data that tells it what you wanted to do. C source code is interpreted by a C compiler as data describing an object file you want it to create. An object file is treated by the loader as data describing some machine instructions you want to queue up for execution. Machine instructions are interpreted by the CPU as data defining the sequence of state transitions it should undergo.

解释型语言通常包含将数据视为代码的机制,这意味着您可以将代码以某种形式传递给函数,然后执行它——甚至在运行时生成代码:

Interpreted languages often contain mechanisms for treating data as code, which means you can pass code into a function in some form and then execute it -- or even generate code at run time:

#!/usr/bin/perl
# Note that the above line explicitly defines the interpretive context for the
# rest of this file.  Without the context of a Perl interpreter, this script
# doesn't do anything.
sub foo {
    my ($expression) = @_;
    # $expression is just a string that happens to be valid Perl

    print "$expression = " . eval("$expression") . "\n";
}

foo("1 + 1 + 2 + 3 + 5 + 8");              # sum of first six Fibonacci numbers
foo(join(' + ', map { $_ * $_ } (1..10))); # sum of first ten squares

像scheme这样的一些语言有一个一等函数"的概念,这意味着你可以将一个函数视为数据并传递它而不用评估它,直到你真的想要.

Some languages like scheme have a concept of "first-class functions", which means that you can treat a function as data and pass it around without evaluating it until you really want to.

结果是代码"之间的划分被取消了.和数据"几乎是任意的,只是透视功能.抽象级别越低,智能"越低.代码必须是:它必须包含有关如何执行的更多信息.另一方面,解释器提供的信息越多,代码就越愚蠢,直到它开始看起来像完全没有智能的数据.

The upshot is that the division between "code" and "data" is pretty much arbitrary, a function of perspective only. The lower the level of abstraction, the "smarter" the code has to be: it has to contain more information about how it should be executed. On the other hand, the more information the interpreter supplies, the more dumb the code can be, until it starts to look like data with no smarts at all.

编写代码最有效的方法之一是简单描述您的需求:将转换为代码的数据,描述如何通过解释性上下文获得您需要的内容.我们称之为声明式编程".

One of the most powerful ways to write code is as a simple description of what you need: Data which will be turned into code describing how to get you what you need by the interpretive context. We call this "declarative programming".

举一个具体的例子,考虑一下 HTML.HTML 不描述图灵完备的编程语言.它只是结构化数据.它的结构包含一些智能,可以让它控制其解释上下文的行为——但不是很多智能.另一方面,它比普通网页上出现的文本段落包含更多智能:这些都是非常愚蠢的数据.

For a concrete example, consider HTML. HTML does not describe a Turing-complete programming language. It is merely structured data. Its structure contains some smarts that let it control the behavior of its interpretive context -- but not a lot of smarts. On the other hand, it contains more smarts than the paragraphs of text that appear on an average web page: Those are pretty dumb data.

这篇关于“数据只是愚蠢的代码,而代码只是智能数据"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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