I heard some of my classmates talking about how they used the function gensym for that, I asked them what it did and even checked up online but I literally can't understand what this function does neither why or when is best to use it.
In particular, I'm more interested in what it does in Lisp. Thank you all.
Unique and uninterned symbols
GENSYMcreates unique symbols. The symbol is also not interned in a package (thus it can't be looked up viaFIND-SYMBOLusing a string as a name). Each call creates a new symbol. The symbol usually has a name which includes a number, which is counted up. The name is also unique (the symbol itself is already unique) with a number, so that a human reader can identify different uninterned symbols in the source code.GENSYM is often used to generate hidden symbols for macros.
gensymis often used in Lisp macros for code generation, when the macro needs to create new identifiers, which then don't clash with existing identifiers.Example: we are going to double the result of a Lisp form and we are making sure that the Lisp form itself will be computed only once. We do that by saving the value in a local variable. The identifier for the local variable will be computed by
gensym.Seeing which symbols are identical
We can also let the printer show which symbols are identical:
The symbol gets a label, and the labels are referenced in the output.