UP | HOME

homoiconicity

1. Lisp S-expressions

An S-expression in Lisp is a list where the first element is (always?) a function

S-expressions can be evaluated to call the function using the rest of the list as arguments.

(+ 2 3)

The arguments can themselves be S-expressions, which will be recursively evalutated. Numbers are "self-evaluating", and always return themselves. Variables will return their value they are bound to

(+ (* 3 4) (* 2 1))

But we can also use the quote operator to prevent the S-expression from being evaluated. Then, it is treated like a plain old list that we can perform our usual list operations on

(cdr (quote (+ 2 3)))

This is what homoiconicity means. You can pass this list around and even modify it (although I'm told that is bad style). Any list that represents a valid s expression can be evaluated:

(eval (quote (+ 2 3)))

eval will first evaluate the (quote ...) S expression to yield the list. It will then evaluate again on the resulting list.

1.1. example

The following gives an expression when evaluated:

(mapcar 'car '((+ 2 3) (2 1) (3 3)))

which can then itself be evaluated:

(eval (mapcar 'car '((+ 2 3) (2 1) (3 3))))

2. sources

Created: 2024-07-15 Mon 01:28