Saturday, November 15, 2008

Code and Data Equivalence

That code and data are equivalent is a common idea in common lisp. However any programmer who has started his programming in any language other than lisp may find this idea alien to him. This happened to me, and it took me some time to understand the concept. This post is all about my understanding about “code data equivalence” in lisp.

First of all what is code? Code is anything that can be executed in a computer. In other words code is what we write in a programming language. Code is as the text books suggest is a human readable representation of what computers going to execute. Like the famous hello world program that all of us has written at some point of our life.

Data on the other hand is something which will be processed by the code. So the code will execute on a data and data change from one execution cycle to another. Data can be given to a code from various sources, like input from a user, file or even a program or code. So in our mind there is a difference. For example take the example of a function which will add two numbers passed to it.

int add(int a, int b)

{

return a+b;

}

In mathematics we know that the operation summation will do *something* with two (or more) numbers and will return a result. This result also will be a number. So the operation of addition is important than that of the data that it operates on. This creates a difference of treating data and code. This difference is the underpinning of treating data and code as separate entities which is true for most of the programming languages.

But in case of lisp this is not true.

In lisp anything that comes to the REPL is s-expression. What is s-expression? Sexp or s-expression is defined in lisp as an atom or list. Atoms are number, string, character etc. So when a input or data come to REPL it evaluates that as any one these alternatives.

The code that we write in lisp is also s-expression. Now as in mathematics we know that:

If A = B and C = B so A = C.

For lisp:

CODE = SEXP and DATA = SEXP so CODE = DATA

QED.