Page 1 of 1
To merge new symbols in macros
Posted: Sun Aug 16, 2015 11:23 am
by J.Owlsteam
Here I am

So... as said here:
viewtopic.php?f=2&t=4484&p=12422#p12422 I wuold like to ask you, I hope, a very little question... that is:
I wuold be pleased to generate, as a macro result, a compound word merged from the union of a variable with some other custom defined content. To better explain, having for example the symbol foo as input of a macro, I wuold like to produce foo-othercontent as output, or better, to use the symbol foo-othercontent inside of the macro. Does lisp have such a potential to do also that? Using the comma operator I got a blank space as, I guess, the variable is treated as a whole new list element. I've also thought about converting a string into a symbol if it is possible, but I don't know how beatiful such a solution could be... do you have any advices?
Re: To merge new symbols in macros
Posted: Sun Aug 16, 2015 12:30 pm
by edgar-rft
J.Owlsteam wrote:...a compound word merged from the union of a variable with some other custom defined content...
What is a "word"? A symbol, a string, (or maybe a 16-bit integer)? What data type do you need as result?
The easiest way to combine all sort of stuff into a string is using (
FORMAT nil ...) like this:
Code: Select all
(format nil "~a ~a ~a" 'this "is an integer:" 123) => "THIS is an integer: 123"
To make a symbol from a string use
INTERN like this:
Take care that there are no spaces and no lowercase character in the string, otherwise you will get symbols with bars:
The bars tell the Common Lisp reader that this is a valid Common Lisp symbol with unusual syntax. You can switch Common Lisp to use lowercase or even mixed-case symbols, but this will give problems with many programs. In case of doubt use
STRING-UPCASE like this:
Code: Select all
(intern (string-upcase "Hello")) => HELLO
Tho get a symbol's name as string use
SYMBOL-NAME like this:
Anything forgotten?
- edgar
Re: To merge new symbols in macros
Posted: Sun Aug 16, 2015 1:56 pm
by J.Owlsteam
Well... What I need is a symbol, I wuold like to compose it with different words united by a -, for example: foo-bar, one of wich is an already existing symbol and the other is added to form the new one. Exactly as, calling (defstruct struct, lisp defines the constructor make-struct.
The function intern seems to be quite interesting but I'm not sure to have understood how it works, since it appears not to return a pointer to the symbol... and the (format nil wuold fit perfectly to my issue, if with intern I could obtain what I seek...
Re: To merge new symbols in macros
Posted: Sun Aug 16, 2015 2:04 pm
by J.Owlsteam
I correct myself, intern seems perfect to be used inside of a macro :) so many thank again, I'll give news in the previous topic! ;)
Re: To merge new symbols in macros
Posted: Sun Aug 16, 2015 2:34 pm
by edgar-rft
If you want to make it look really scary you could add some
FORMAT science:
Code: Select all
(defun symbol-from-items (&rest items)
(intern (format nil "~:@(~a~{-~a~}~)" (first items) (rest items))))
(symbol-from-items 1 2 3 'hello "kitty") => 1-2-3-HELLO-KITTY
See
~:@( ~),
~{ ~}, and
~a if you want to use it.
- edgar
Re: To merge new symbols in macros
Posted: Sun Aug 16, 2015 3:00 pm
by J.Owlsteam
Uhm uhm... maybe... tomorrow, better avoid to have my dreams infested with dreadful nightmares of recursive parenthesis

Re: To merge new symbols in macros
Posted: Wed Aug 19, 2015 5:20 am
by Goheeca
That can be simplified to:
Code: Select all
(defun symbol-from-items (&rest items)
(intern (format nil "~:@(~{~a~^-~}~)" items)))
(symbol-from-items 1 2 3 'hello "kitty") => 1-2-3-HELLO-KITTY
I've created an
read-time template library and with that it should be straightforward to do this symbol building and more complicated tweaks. Yeah, it has its own limits for sure.
Here is how to install it with quicklisp (it's a mirror site, originally it was
there).