Why code-as-data?

Peter C picture Peter C · Nov 10, 2010 · Viewed 9.4k times · Source

What is code-as-data? I've heard it's superior to "code-as-ascii-characters" but why? I personally find the code-as-data philosophy a bit confusing actually.

I've dabbled in Scheme, but I never really got the whole code-as-data thing and wondered what exactly does it mean?

Answer

I GIVE CRAP ANSWERS picture I GIVE CRAP ANSWERS · Nov 10, 2010

It means that your program code you write is also data which can be manipulated by a program. Take a simple Scheme expression like

(+ 3 (* 6 7))

You can regard it as a mathematical expression which when evaluated yields a value. But it is also a list containing three elements, namely +, 3 and (* 6 7). By quoting the list,

 '(+ 3 (* 6 7))

You tell scheme to regard it as the latter, namely just a list containing three elements. Thus, you can manipulate this list with a program and then evaluate it. The power it gives you is tremendous, and when you "get" the idea, there are some very cool tricks to be played.