Variable references in lisp

With lexical scope one does not have access to variables that are not in the current scope. You also cannot pass lexical variables to other functions directly. Lisp evaluates variables and passes the values bound to these variables. There is nothing like first-class references to variables. Think functional! (let ((a 1)) (values (lambda (new-value) (setf … Read more

Lisp Executable

I was actually trying to do this today, and I found typing this into the CLisp REPL worked: (EXT:SAVEINITMEM “executable.exe” :QUIET t :INIT-FUNCTION ‘main :EXECUTABLE t :NORC t) where main is the name of the function you want to call when the program launches, :QUIET t suppresses the startup banner, and :EXECUTABLE t makes a … Read more

setq and defvar in Lisp

There are several ways to introduce variables. DEFVAR and DEFPARAMETER introduce global dynamic variables. DEFVAR optionally sets it to some value, unless it is already defined. DEFPARAMETER sets it always to the provided value. SETQ does not introduce a variable. (defparameter *number-of-processes* 10) (defvar *world* (make-world)) ; the world is made only once. Notice that … Read more

How to compare two functions for equivalence, as in (λx.2*x) == (λx.x+x)?

It’s pretty well-known that general function equality is undecidable in general, so you’ll have to pick a subset of the problem that you’re interested in. You might consider some of these partial solutions: Presburger arithmetic is a decidable fragment of first-order logic + arithmetic. The universe package offers function equality tests for total functions with … Read more

What makes Lisp macros so special?

To give the short answer, macros are used for defining language syntax extensions to Common Lisp or Domain Specific Languages (DSLs). These languages are embedded right into the existing Lisp code. Now, the DSLs can have syntax similar to Lisp (like Peter Norvig’s Prolog Interpreter for Common Lisp) or completely different (e.g. Infix Notation Math … Read more

tail-recursive function appending element to list

The following is an implementation of tail recursion modulo cons optimization, resulting in a fully tail recursive code. It copies the input structure and then appends the new element to it, by mutation, in the top-down manner. Since this mutation is done to its internal freshly-created data, it is still functional on the outside (does … Read more