Reputation: 2992
I'm trying to understand the view that procedures and data are virtually the same in lisp. SICP says that:
The second stipulation says that "the values of built-in operators are the machine instruction sequences that carry out the corresponding operations." if I wish to change the values thereby changing the machine instructions, like this:
(define + 2)
(* + 3) ;6
it works fine.
Now, the first case stipulates that "the values of numerals are the numbers that they name". If I type
2
The value is the representation of 2 that gets outputted. Now, if I wish to change it, like this:
(define 2 +) ;bad syntax
Why is that?
Upvotes: 0
Views: 62
Reputation: 139251
procedures and data are virtually the same in lisp
That's not the case. Procedures are data. One kind of data. There are other kinds of data: numbers, strings, cons cells, symbols, vectors, ...
In a Scheme program, a symbol is a variable. You can modify the binding between the variable and a value. That's why you might be able to change the binding of the variable +
.
But it is not possible to change 2
, since it is a number and not a variable. A number has no binding. The value of 2
is already 2
.
Upvotes: 2
Reputation: 2659
In any definition of a language, you need a distinction between symbols (if you have symbols) or names, and literals (in languages which only have literals, some of which code for instructions, this is not necessary.)
In the lisp in your example, 2
and 3.1415
are literals - +
may be a symbol. Thus, it makes sense that you can re-assign a symbol, or name, to point somewhere else. Literals, however, will always be data. They are distinct from names.
You could define a language where literals are, just like +
, symbols mapped to a default value, able to be re-mapped if you wish. An example of such a language might be the C pre-processor.
EDIT: More generally, you comment was about how "code is data" in lisp. Your example is a bad example of this feature - it is a question which concerns the parser or grammar of the language and its tokens.
A better example would be the following:
(+ 2 3)
This expression is both an executable statement, and a data structure. Namely it can code for "a function invocation which returns 5
" or, if prefixed by a "do not evaluate me straight away '
" the pair structure
(+ . (2 . (3 . NIL)))
which is the standard linked list structure found in lisp implementations. You can pass this data structure around in code, execute it later with something like the EVAL
function, or (most powerfully), edit it so that it does something else and then execute it.
This is possible because lisps are homoiconic - the source code works both as code and data.
Upvotes: 2