I want to insert a char into a list. However, I want to merge this char with the last symbol in the list. With appends and cons the result is always two different symbols. Well, I want one merged symbol to be my result.
Example:
(XXXX 'a '5) ====> (a5)
What I would like to have, instead of:
(XXXX 'a '5) ====> (a 5)
You cannot "merge symbols" in Lisp.
First of all,
5
is not a symbol, but a number. If you want a symbol named"5"
you have to type it as|5|
(for example).If a function takes the symbol
A
and symbol|5|
, and produces the symbolA5
, it has not merged symbols. It has created a new symbol whose name is the catenation of the names of those input symbols.Properly designed Lisp programs rarely depend on how a symbol is named. They depend on symbols being unique entities.
If you're using symbols to identify things, and both
5
andA
identify some entity, the best answer isn't necessarily to create a new symbol which is, in name at least, is a mashup of these two symbols. For instance, a better design might be to accept that names are multi-faceted or compound in some way. Perhaps the list(A 5)
can serve as a name.Common Lisp functions themselves can have compound names. For instance
(setf foo)
is a function name. Aggregates like lists can be names.If you simply need the machine to invent unique symbols at run-time, consider using the
gensym
function. You can pass your own prefix to it:Of course, the prefix can be the name of some existing symbol, pulled out via
symbol-name
. The symbol#:FOO0042
is not unique because of the0042
but because it is a freshly allocated object in the address space. The#:
means it is not interned in any package. The name of the symbol isFOO0042
.If you still really want to, a simple way to take the printed representation of a bunch of input objects and turn it into a symbol is this:
Examples:
Define this:
and then use it as:
If you expect your arguments to contain stuff besides symbols, then replace
symbol-name
with:or a pre-existing CL function (that I've forgotten :() that stringifies anything.
The answer to the question you ask is
Oh, if you want a list as the result:
However, I am pretty sure this is not the question you actually want to ask.
Creating new symbols programmatically is not something beginners should be doing...