Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Lisp, code is data. What benefit does that provide?

Tags:

lisp

In Lisp, any program's code is actually a valid data structure. For example, this adds one and two together, but it's also a list of three items.

(+ 1 2) 

What benefit does that provide? What does that enable you to do that's impossible and/or less elegant in other languages?

like image 522
ClosureCowboy Avatar asked Apr 29 '11 13:04

ClosureCowboy


People also ask

What does code as data mean?

In computer science, the expressions code as data and data as code refer to the duality between code and data, that allows computers to treat instructions in a programming language as data handled by a running program.

What are Lisp macros good for?

The Common Lisp macro facility allows the user to define arbitrary functions that convert certain Lisp forms into different forms before evaluating or compiling them. This is done at the expression level, not at the character-string level as in most other languages.

What is Lisp explain basic Lisp functions in details?

LISP represents a function call f(x) as (f x), for example cos(45) is written as cos 45. LISP expressions are case-insensitive, cos 45 or COS 45 are same. LISP tries to evaluate everything, including the arguments of a function. Only three types of elements are constants and always return their own value. Numbers.

How Do You Use a LISP code?

Steps to run Lisp programs on CUIT or CS machines: Step 0: Login into your account. Step 1: Execute "lisp" on the CUIT machines to enter Allegro Common Lisp environment. Alternatively, execute "clisp" on the CS machines to start GNU CLISP. Step 2: Use "load" function to load lisp files into lisp environment.


2 Answers

To make things a little clearer with respect to code representation, consider that in every language code is data: all you need is strings. (And perhaps a few file operations.) Contemplating how that helps you to mimic the Lisp benefits of having a macro system is a good way for enlightenment. Even better if you try to implement such a macro system. You'll run into the advantages of having a structured representation vs the flatness of strings, the need to run the transformations first and define "syntactic hooks" to tell you where to apply them, etc etc.

But the main thing that you'll see in all of this is that macros are essentially a convenient facility for compiler hooks -- ones that are hooked on newly created keywords. As such, the only thing that is really needed is some way to have user code interact with compiler code. Flat strings are one way to do it, but they provide so little information that the macro writer is left with the task of implementing a parser from scratch. On the other hand, you could expose some internal compiler structure like the pre-parsed AST trees, but those tend to expose too much information to be convenient and it means that the compiler needs to somehow be able to parse the new syntactic extension that you intend to implement. S-expressions are a nice solution to the latter: the compiler can parse anything since the syntax is uniform. They're also a solution to the former, since they're simple structures with rich support by the language for taking them apart and re-combining them in new ways.

But of course that's not the end of the story. For example, it's interesting to compare plain symbolic macros as in CL and hygienic macros as in Scheme implementations: those are usually implemented by adding more information to the represented data, which means that you can do more with those macro system. (You can also do more with CL macros since the extra information is also available, but instead of making it part of the syntax representation, it's passed as an extra environment argument to macros.)

like image 198
Eli Barzilay Avatar answered Sep 21 '22 15:09

Eli Barzilay


My favorite example... In college some friends of mine were writing a compiler in Lisp. So all their data structures including the parse tree was lisp s-expressions. When it was time to implement their code generation phase, they simply executed their parse tree.

like image 31
ReinstateMonica Larry Osterman Avatar answered Sep 24 '22 15:09

ReinstateMonica Larry Osterman