Code Comments
Programming Forum and web based access to our favorite programming groups.Are there any ways to manipulate symbols analogously to what can be done with strings? I mean, for instance, is it possible to concatenate two symbols like 'my and 'sum into 'mysum like you can with strings as follows? (concatenate 'string "my" "sum") I know for example that a string can be converted to a symbol with the "intern" function (though I've discovered through using it, that you've really got to be careful with case!) But I'm not aware of any particular way of operating directly on the symbols. Thank you.
Post Follow-up to this messageOn Mar 26, 9:17 pm, rocco.ro...@gmail.com wrote: > Are there any ways to manipulate symbols analogously to what can be > done with strings? I mean, for instance, is it possible to concatenate > two symbols like 'my and 'sum into 'mysum like you can with strings as > follows? > > (concatenate 'string "my" "sum") > > I know for example that a string can be converted to a symbol with the > "intern" function (though I've discovered through using it, that > you've really got to be careful with case!) But I'm not aware of any > particular way of operating directly on the symbols. > > Thank you. You can get the (string) name of a symbol using SYMBOL-NAME. So for your example the code would be (intern (concatenate 'string (symbol-name 'my) (symbol-name 'sum))) Michael
Post Follow-up to this messageOn Mar 26, 8:46 pm, Michael Ben-Yosef <septa...@mweb.co.za> wrote: > On Mar 26, 9:17 pm, rocco.ro...@gmail.com wrote: > > > > > > You can get the (string) name of a symbol using SYMBOL-NAME. So for > your example the code would be > (intern (concatenate 'string (symbol-name 'my) (symbol-name 'sum))) > > Michael Thanks Michael. So it seems I do have to do some coverting though after all.
Post Follow-up to this messagerocco.rossi@gmail.com writes: > Are there any ways to manipulate symbols analogously to what can be > done with strings? I mean, for instance, is it possible to concatenate > two symbols like 'my and 'sum into 'mysum like you can with strings as > follows? > > (concatenate 'string "my" "sum") > > I know for example that a string can be converted to a symbol with the > "intern" function (though I've discovered through using it, that > you've really got to be careful with case!) But I'm not aware of any > particular way of operating directly on the symbols. A symbol in CL is also a designator for the string that is its name; see the full description here: http://www.lispworks.com/documentat...ignat or This doesn't help in the CONCATENATE case, but many other string-specific functions take string designators, and therefore symbols, when it's convenient. Zach
Post Follow-up to this messagerocco.rossi@gmail.com writes: > Are there any ways to manipulate symbols analogously to what can be > done with strings? Why do you want to do this? -- Frode Vatvedt Fjeld
Post Follow-up to this message> Thanks Michael. So it seems I do have to do some coverting though > after all. The point of interned symbols is that their names as strings are no longer required to determine their identity as objects. For example, to check if two interned symbols are equal, Lisp doesn't need to compare their names as strings. Hence the name of a symbol is really just one of it's properties as an object. (See the definition for the SYMBOL class, here: http://www.lispworks.com/documentat...bol .htm ) Two different symbols can have the same name if they are interned in different packages (or uninterned), for example 'CL:DEFUN and :DEFUN both have the name "DEFUN". What I'm getting at is that symbols may not be the right kind of objects to use if you want to do a lot of slicing, dicing and splicing with their names. You probably want to stick to strings until you have one you really need to intern as a symbol. In any case, as Xach pointed out, in many functions you can use a symbol in place of its name. Michael
Post Follow-up to this messagerocco.rossi@gmail.com writes:
> Are there any ways to manipulate symbols analogously to what can be
> done with strings? I mean, for instance, is it possible to concatenate
> two symbols like 'my and 'sum into 'mysum like you can with strings as
> follows?
>
> (concatenate 'string "my" "sum")
>
> I know for example that a string can be converted to a symbol with the
> "intern" function (though I've discovered through using it, that
> you've really got to be careful with case!) But I'm not aware of any
> particular way of operating directly on the symbols.
Well the classical way of processing symbols is to use EXPLODE and
IMPLODE:
(implode (append (explode 'my) (explode 'sum))) --> MYSUM
Unfortunately { ;-) } implode and explode didn't make it to Common
Lispš, so you have to implement them yourself:
(defun explode (symbol)
(map 'list (lambda (ch) (intern (string ch))) (string symbol)))
(defun implode (list-of-one-char-symbols)
(intern (format nil "~{~A~}" list-of-one-char-symbols )))
But still the question remains, why you would want to do such a thing,
now that we have true strings in lisp?
šimplode and explode existed in lisp at a time when there was no
character and no string types. Characters were represented as
one-character symbols and strings as symbols.
(explode 'mysum) --> (M Y S U M)
--
__Pascal Bourguignon__ http://www.informatimago.com/
Kitty like plastic.
Confuses for litter box.
Don't leave tarp around.
Post Follow-up to this messagerocco.rossi@gmail.com writes: > Are there any ways to manipulate symbols analogously to what can be > done with strings? I mean, for instance, is it possible to concatenate > two symbols like 'my and 'sum into 'mysum like you can with strings as > follows? Well, since a symbol is an object with various properties such as associated values, functions (i.e., SYMBOL-VALUE, SYMBOL-FUNCTION), it isn't immediately obvious what it would mean to concatenate such objects. That is fundamentally why there isn't such a function. If you want to concatenate only the SYMBOL-NAME of the symbols, then you need to specify that. > (concatenate 'string "my" "sum") > > I know for example that a string can be converted to a symbol with the > "intern" function (though I've discovered through using it, that > you've really got to be careful with case!) But I'm not aware of any > particular way of operating directly on the symbols. Well, that's because the results are a bit underspecified. By the way, you can also use MAKE-SYMBOL, if you don't want the symbol to be interned. However, it isn't that hard to write your own function: (defun symbol-concatenate (&rest symbols) (intern (apply #'concatenate 'string (mapcar #'symbol-name symbols)))) or if you only need to do two symbols: (defun symbol-concatenate2 (symbol1 symbol2) (intern (concatenate 'string (string symbol1) (string symbol2)))) -- Thomas A. Russ, USC/Information Sciences Institute
Post Follow-up to this messageOn Mar 26, 10:16 pm, t...@sevak.isi.edu (Thomas A. Russ) wrote: > rocco.ro...@gmail.com writes: > > Well, since a symbol is an object with various properties such as > associated values, functions (i.e., SYMBOL-VALUE, SYMBOL-FUNCTION), it > isn't immediately obvious what it would mean to concatenate such > objects. That is fundamentally why there isn't such a function. > > If you want to concatenate only the SYMBOL-NAME of the symbols, then you > need to specify that. > > > > Well, that's because the results are a bit underspecified. By the way, > you can also use MAKE-SYMBOL, if you don't want the symbol to be > interned. > > However, it isn't that hard to write your own function: > > (defun symbol-concatenate (&rest symbols) > (intern (apply #'concatenate 'string (mapcar #'symbol-name symbols)))) > > or if you only need to do two symbols: > > (defun symbol-concatenate2 (symbol1 symbol2) > (intern (concatenate 'string (string symbol1) (string symbol2)))) > > -- > Thomas A. Russ, USC/Information Sciences Institute Thank you all for kindly helping me out with this issue. I realize it appears to be a very strange thing someone would want to do :) but strangely enough the need for it arose while I was working on a GUI and I needed to automatically generate labels like button1, button2, button3, and so on ... I could've done it by hand, but I thought to myself "this is what Lisp is all about, being able to abstract certain patterns out", so there should be some reasonable way of automagically generating those labels, say with a loop over i, and concatenating "button" with i, and thus "interning" the resulting symbol. Then, one thing leads to another, I wondered if one could manipulate symbol names (generating new symbols) like strings, and that's the reason for this post :) Thanks again.
Post Follow-up to this message> From: rocco.ro...@gmail.com > I realize it appears to be a very strange thing someone would > want to do :) but strangely enough the need for it arose while I > was working on a GUI and I needed to automatically generate labels > like button1, button2, button3, and so on ... Normally the label on a button is a string. Why would you want it to be a symbol instead? How could a symbol even work for that purpose? Here is Common Lisp evidence: <http://kantz.com/jason/clim-primer/panes.htm> (make-pane 'push-button :label "squeeze" :activate-callback #'squeeze) (make-pane 'push-button :label "press" :activate-callback #'press))) See, the label on each push-button is a string, not a symbol. <http://osdir.com/ml/lisp.mcclim.cvs...2/msg00005.html> \CLIM{} comes with a set of predefined gadget panes. They consist of push-button, toggle-button, slider, radio-box, text-field, text-editor panes ready for use by the \CLIM{} application programmer. ... (I cited that just so you'd know how push-button fit into larger picture.) The same is true in Java, but perhaps more clearly documentated: <http://java.sun.com/j2se/1.3/docs/a...awt/Button.html> Constructor Summary Button(String label) Constructs a Button with the specified label. Method Summary String getLabel() Gets the label of this button. void setLabel(String label) Sets the button's label to be the specified string. <http://java.sun.com/j2se/1.3/docs/a...ng/JButton.html> Constructor Summary JButton(String text) Creates a button with text. Hmm, it's called "text" instead of "label", sigh. Methods inherited from class javax.swing.AbstractButton <http://java.sun.com/j2se/1.3/docs/a...ractButton.html> public String getLabel() Deprecated. - Replaced by getText Returns the label text. public void setLabel(String label) Deprecated. - Replaced by setText(text) Sets the label text. Likewise in C++: <http://www.informit.com/articles/ar...&seqNum=2&rll=1> QPushButton *quitButton = new QPushButton("Quit", 0); <http://www.daniweb.com/forums/thread77173.html> QPushButto *hello = new QPushButton("This is a simple button"); /* Creates a new button with the label "Hello World". */ button = gtk_button_new_with_label ("Hello World"); <http://doc.trolltech.com/3.3/qpushbutton.html> Public Members * QPushButton ( const QString & text, QWidget * parent, const char * name = 0 ) The QPushButton widget provides a command button. The push button, or command button, is perhaps the most commonly used widget in any graphical user interface. Push (click) a button to command the computer to perform some action, or to answer a question. Typical buttons are OK, Apply, Cancel, Close, Yes, No and Help. A command button is rectangular and typically displays a text label describing its action. An underlined character in the label (signified by preceding it with an ampersand in the text) indicates an accelerator key, e.g. QPushButton *pb = new QPushButton( "&Download", this ); Push buttons can display a textual label or a pixmap, and optionally a small icon. These can be set using the constructors and changed later using setText(), setPixmap() and setIconSet(). ... Important Inherited Members * QString text () const Returns the text shown on the button. See the "text" property for details. * virtual void setText ( const QString & ) Sets the text shown on the button. See the "text" property for details. So you see in CLIM, java.awt, javax.swing, and C++ Qt toolkit, everywhere the textual label for a button is given as a string. Where do you get the idea it should be a symbol instead of a string? > there should be some reasonable way of automagically generating > those labels, say with a loop over i, and concatenating "button" > with i, and thus "interning" the resulting symbol. Huh? You can't concatenate a string with an integer. Perhaps you mean you want to convert the integer to its decimal representation as a string, and then concatenate "button" with that decimal representation string? But even if you did that, the result would be a string, not a symbol, and "thus" is totally wrong because concatenating doesn't do interning so it wouldn't "thus" intern the string. If you wanted to convert the string to an interned symbol, that would be an extra step *after* concatenating. But there's no need to convert the integer i to a string and *then* concatenate the string "button" in front of it. Do it all in one step: (format NIL "button~A" i) > Then, one thing leads to another, I wondered if one could > manipulate symbol names (generating new symbols) like strings, ... Symbol names *are* strings!! So manipulating symbol names isn't *like* manipulating strings, it **is* manipulating strings! But manipulating symbol names (i.e. strings) doesn't generate any new symbols, it just generates new strings. I suggest you go back to the documentation for whatever GUI you're trying to generate buttons for, and read carefully what it requires to be passed to it as the name of a button. If it requires passing a symbol, and forbids passing a string, that would be very surprising. If you believe it says that, quote that specification from the documentation so that we can see if you are understanding it correctly.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.