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

How to generate all the permutations of elements in a list one at a time in Lisp?

Here’s a way (following the code structure by @coredump from their answer; runs about 4x faster on tio.run): (defun permutations (list callback) (if (null list) (funcall callback #()) (let* ((all (cons ‘head (copy-list list))) ; head sentinel FTW! (perm (make-array (length list)))) (labels ((g (p i &aux (q (cdr p))) ; pick all items in … Read more

values function in Common Lisp

Multiple Values in CL The language Common lisp is described in the ANSI standard INCITS 226-1994 (R2004) and has many implementations. Each can implement multiple values as it sees fit, and they are allowed, of course, to cons up a list for them (in fact, the Emacs Lisp compatibility layer for CL does just that … Read more