To merge new symbols in macros
-
- Posts: 21
- Joined: Wed Jul 29, 2015 7:25 am
To merge new symbols in macros
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?
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
What is a "word"? A symbol, a string, (or maybe a 16-bit integer)? What data type do you need as result?J.Owlsteam wrote:...a compound word merged from the union of a variable with some other custom defined content...
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"
Code: Select all
(intern "HELLO") => HELLO
Code: Select all
(intern "Hello") => |Hello|
Code: Select all
(intern (string-upcase "Hello")) => HELLO
Code: Select all
(symbol-name 'hello) => "HELLO"
- edgar
-
- Posts: 21
- Joined: Wed Jul 29, 2015 7:25 am
Re: To merge new symbols in macros
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...
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...
-
- Posts: 21
- Joined: Wed Jul 29, 2015 7:25 am
Re: To merge new symbols in macros
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
If you want to make it look really scary you could add some FORMAT science:
See ~:@( ~), ~{ ~}, and ~a if you want to use it.
- edgar
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
- edgar
-
- Posts: 21
- Joined: Wed Jul 29, 2015 7:25 am
Re: To merge new symbols in macros
Uhm uhm... maybe... tomorrow, better avoid to have my dreams infested with dreadful nightmares of recursive parenthesis
Re: To merge new symbols in macros
That can be simplified to:
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).
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
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.