To merge new symbols in macros

Discussion of Common Lisp
Post Reply
J.Owlsteam
Posts: 21
Joined: Wed Jul 29, 2015 7:25 am

To merge new symbols in macros

Post by J.Owlsteam » Sun Aug 16, 2015 11:23 am

Here I am :D
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?

edgar-rft
Posts: 226
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: To merge new symbols in macros

Post by edgar-rft » Sun Aug 16, 2015 12:30 pm

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:

Code: Select all

(intern "HELLO") => HELLO
Take care that there are no spaces and no lowercase character in the string, otherwise you will get symbols with bars:

Code: Select all

(intern "Hello") => |Hello|
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:

Code: Select all

(symbol-name 'hello) => "HELLO"
Anything forgotten?

- edgar

J.Owlsteam
Posts: 21
Joined: Wed Jul 29, 2015 7:25 am

Re: To merge new symbols in macros

Post by J.Owlsteam » Sun Aug 16, 2015 1:56 pm

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...

J.Owlsteam
Posts: 21
Joined: Wed Jul 29, 2015 7:25 am

Re: To merge new symbols in macros

Post by J.Owlsteam » Sun Aug 16, 2015 2:04 pm

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! ;)

edgar-rft
Posts: 226
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: To merge new symbols in macros

Post by edgar-rft » Sun Aug 16, 2015 2:34 pm

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

J.Owlsteam
Posts: 21
Joined: Wed Jul 29, 2015 7:25 am

Re: To merge new symbols in macros

Post by J.Owlsteam » Sun Aug 16, 2015 3:00 pm

Uhm uhm... maybe... tomorrow, better avoid to have my dreams infested with dreadful nightmares of recursive parenthesis :mrgreen:

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: To merge new symbols in macros

Post by Goheeca » Wed Aug 19, 2015 5:20 am

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).
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.

Post Reply