Code is data....

Started by
30 comments, last by DerPhill 18 years, 3 months ago
Oy.... I'm reading up on XML which somehow reminded me of some thread on XML vs. Lisp (oh the good ol' days). Now I remember, Sabreman in particular, people saying that in Lisp "code is data and data is code". Unfortunately, those same people were right in saying that the concept transcends my plane of thinking. OK. This is code:

struct People {

   int age = 16;
   string name = "John Smith";
   float height = 70; // this is in inches
   Gender sex = MALE; // this is an enum

} Person;
but isn't this data as well? (A lisp example [and comparison] would be nice.) I guess I'm not seeing how Lisp satisfies that philosophy/criteria and C/C++/C#/Java doesn't.

Beginner in Game Development?  Read here. And read here.

 

Advertisement
In C:
if (a == b)    print("Hello.");else    print("Goodbye.");


That text will be read in, tokenized by the lexer, parsed by the parser, and compiled. The data structures that the compiler uses internally at any stage are unknown, and never accessible to the programmer through the code.

In Lisp (some arbitrary dialect - don't be pedantic about this):
(if (eq? a b)  (print "Hello.")  (print "Goodbye."))


That text will be read in by the lisp 'reader'. This will create a set of Lisp objects (in this case: lists, containing symbols and strings). Because they are normal lisp objects (the programmer knows exactly what objects/data structures will be created), if there is an appropriate interface in the Lisp system (generally speaking: macros), then it is possible for the programmer to write code which will process these objects (ie, code that manipulates 'code') The lisp compiler will then process these objects to do the necessary optimizations and conversion to machine code.

Does that help at all? I agree it's very badly explained most of the time - lispers have a tendency to say "in lisp, code is data and data is code" as if it's some magic phrase that will make everyone bow down before them, without ever explaining what they mean.

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
Quote:Original post by Alpha_ProgDes
Oy....
I'm reading up on XML which somehow reminded me of some thread on XML vs. Lisp (oh the good ol' days). Now I remember, Sabreman in particular, people saying that in Lisp "code is data and data is code". Unfortunately, those same people were right in saying that the concept transcends my plane of thinking.

OK. This is code:
struct People {   int age = 16;   string name = "John Smith";   float height = 70; // this is in inches   Gender sex = MALE; // this is an enum} Person;

but isn't this data as well? (A lisp example [and comparison] would be nice.)

I guess I'm not seeing how Lisp satisfies that philosophy/criteria and C/C++/C#/Java doesn't.


That's not what they mean. They are saying the actual code itself is data, not what the code represents. One thing that is particularly handy for is metaprogramming.
Metaprogramming?

Like... Programming with your inner chi? :D
----------------------------------------------------------Rating me down will only make me stronger.----------------------------------------------------------
A metaprogram is a program that creates programs. Code that writes code.
I like this topic. Though i'm somewhat of a beginner (5 months c++), I'm very interested in functional programming and want to learn this in the future. I talked with my stepfather about this kind of programming, and he gave me some book about it.
Maybe I can contribute with a quote:
Quote:The two approaches, imperative and definitional, can be illustrated by the use of an analogy in which one is asked to specify a physical object.
(...)
(imperative:) To build a shed:
a) Lay the foundations;
b) Build the walls;
c) Lay the floor;
d) Put the roof on.

(descriptive / functional:) A shed consists of:
a) Walls supported by the foundations;
b) A floor supported by the foundations;
c) A roof supported by the walls.

(...) Thus the explicit ordering of actions required in the first description has been replaced by an implicit ordering, conditioned by the relationships between the objects, in the second description. (...) Since the idea of sequencing through a series of statements is not very meaningful in a functional language, not only is it not important in which order a set of statements is written, but also we would not expect to find any statements that attempted to disturb a possible sequence. Thus functional languages do not have such control structures.

Principles of functional programming, Glaser, Hankin and Till

All this sounds very wonderful to me, but i still have problems grokking how one would write a somewhat larger project in this way. I think that is mainly due to the lack of control structures.

Well, I hope this makes sense. Happy holidays / christmas / whatever
Code is data means a lot more than that you can write code that contains data.

First one question: How would you read the data in your example?


For an example what you can do when code is data, look at this: (from Practical Common Lisp)
(define-binary-class id3-tag  ((file-identifier (iso-8859-1-string :length 3))   (major-version   u1)   (revision        u1)   (flags           u1)   (size            id3-tag-size)   (frames          (id3-frames :tag-size size))))(let ((id3 (read-value 'id3-tag myfile)))   (setf (flags id3) 0)   (write-object id3 myotherfile))

define-binary-class is something you can write quite easily when code is data (it is not a builtin feature of Lisp). How would you write something like that in C#/Java?

In Lisp you need a bit of framework code (75 lines or so, click the link) to make this work, but once you have it you just define your class and automatically you can read and write it in exactly the format you specified (or translated from the fileformat specs).
I feel that code is data because you save, update, parse, and manipulate code just as you would any kind of data.
Gor435 - My Journal - MySpace - Facebook
Quote:Original post by Trap
Lisp code

seeing as i barely know any Lisp, that example just went over my head.
that example seemed to first define a data structure (binary file) then under a function was created to read in that data structure (am i right?) but how is that from my struct example?

struct People {   int age;   string name;   float height; // this is in inches   Gender sex; // this is an enum};void setPerson (People& person) {    fileData >> age;     //where fileData points to a text file    fileData >> name;    fileData >> height;    fileData >> sex;}

Beginner in Game Development?  Read here. And read here.

 

Quote:Original post by DeadXorAlive
I like this topic. Though i'm somewhat of a beginner (5 months c++), I'm very interested in functional programming and want to learn this in the future. I talked with my stepfather about this kind of programming, and he gave me some book about it.
Maybe I can contribute with a quote:
Quote:The two approaches, imperative and definitional, can be illustrated by the use of an analogy in which one is asked to specify a physical object.
(...)
(imperative:) To build a shed:
a) Lay the foundations;
b) Build the walls;
c) Lay the floor;
d) Put the roof on.

(descriptive / functional:) A shed consists of:
a) Walls supported by the foundations;
b) A floor supported by the foundations;
c) A roof supported by the walls.

(...) Thus the explicit ordering of actions required in the first description has been replaced by an implicit ordering, conditioned by the relationships between the objects, in the second description. (...) Since the idea of sequencing through a series of statements is not very meaningful in a functional language, not only is it not important in which order a set of statements is written, but also we would not expect to find any statements that attempted to disturb a possible sequence. Thus functional languages do not have such control structures.

Principles of functional programming, Glaser, Hankin and Till

All this sounds very wonderful to me, but i still have problems grokking how one would write a somewhat larger project in this way. I think that is mainly due to the lack of control structures.

Well, I hope this makes sense. Happy holidays / christmas / whatever

Well, the problem is, you can't write large programs this way because declarative and functional programming aren't Turing complete. In the example given above, somewhere along the line something has to tell the builder how a, b, and c interact. Now, the fact that declarative and functional programming aren't turing complete is what makes them so powerful, because they're so much more expressive and easier to understand than turing complete imperative code. Once you realize this, your main goal is to shift as much imperative code over to the declarative/functional side as possible.

This topic is closed to new replies.

Advertisement