3 ms·
Another way of cheating, with reader macros in Common Lisp: USER> #1=(LAMBDA () (WRITE '#1# :CIRCLE T) NIL)
by junke 6y ago
Another way of cheating, with reader macros in Common Lisp:
USER> #1=(LAMBDA () (WRITE '#1# :CIRCLE T) NIL)
#<FUNCTION (LAMBDA ()) {540F153B}>
The function, when evaluated, prints its code:
USER> (funcall *)
#1=(LAMBDA () (WRITE '#1# :CIRCLE T) NIL)
If you read back the printed string, you have an identical function as the first one. The '#n=' binds a reader label n to the AST that follows, and '#n#' is replaced by the AST bound to n, which means your AST can be a circular data-structure.
I added :circle t so that circular structure are printed readably, and the (write ...) is followed by NIL so that the structure is not printed to the REPL during interactive evaluation.
You could also write:
#1=(lambda () (eval '#1#))
When you evaluate the expression you have a #1={function, which, when called, returns a #1#}.