Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird syntax in common lisp

Tags:

common-lisp

I found this lisp function while I was googling

(defun filter (lst items-to-filter)
   (cond ((null lst) nil)
         ((member (car lst) items-to-filter) #1=(filter (cdr lst) items-to-filter))
         (t (cons (car lst) #1#))))

It's just set difference, but this is the first time i see #1= and #1#, syntax. I think I understand what it means just by looking at the code, but I am not too sure. I think the #1= is used to label an expression so as not to retype it later when needed, one can just refer to it by #index#, in this case index=1. I was wondering if someone could shed some light on this. What are these constructs called, if there's a reference for them, and if they are widely used in modern lisp code. Thanks

like image 202
turingcomplete Avatar asked Dec 06 '25 00:12

turingcomplete


1 Answers

To see it in written source code is very very unusual. Most of the time you see it in data. It is used to create or print shared data items in s-expressions. This way you can also read or print circular s-expressions.

You could use it for easier creation of repeated code, but usually one writes functions or macros for that. Functions have the advantage that they save code space - unless they are inlined.

CL-USER 3 > (pprint '(defun filter (lst items-to-filter)
                       (cond ((null lst) nil)
                             ((member (car lst) items-to-filter)
                              #1=(filter (cdr lst) items-to-filter))
                             (t (cons (car lst) #1#)))))

(DEFUN FILTER (LST ITEMS-TO-FILTER)
  (COND ((NULL LST) NIL)
        ((MEMBER (CAR LST) ITEMS-TO-FILTER)
         (FILTER (CDR LST) ITEMS-TO-FILTER))
        (T
         (CONS (CAR LST) (FILTER (CDR LST) ITEMS-TO-FILTER)))))

As you see above the printer does not print it that way. Why is that? There is a global variable *print-circle* which controls it. For above example it was set to NIL. Let's change that:

CL-USER 4 > (setf *print-circle* t)
T

CL-USER 5 > (pprint '(defun filter (lst items-to-filter)
                       (cond ((null lst) nil)
                             ((member (car lst) items-to-filter)
                              #1=(filter (cdr lst) items-to-filter))
                             (t (cons (car lst) #1#)))))

(DEFUN FILTER (LST ITEMS-TO-FILTER)
  (COND ((NULL LST) NIL)
        ((MEMBER (CAR LST) ITEMS-TO-FILTER)
         #1=(FILTER (CDR LST) ITEMS-TO-FILTER))
        (T
         (CONS (CAR LST) #1#))))

So this shows that one can read and print such s-expressions in Common Lisp

Sharing some source code data structures is more common in computed code:

CL-USER 22 > (defmacro add-1-2-3 (n) `(,n 1 2 3))
ADD-1-2-3

CL-USER 23 > (walker:walk-form '(+ (add-1-2-3 4) (add-1-2-3 5)))
(+ (4 . #1=(1 2 3)) (5 . #1#))
like image 112
Rainer Joswig Avatar answered Dec 10 '25 00:12

Rainer Joswig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!