Re: String[bring] vs "bring"
- To: mathgroup at smc.vnet.net
- Subject: [mg59370] Re: String[bring] vs "bring"
- From: albert <awnl at arcor.de>
- Date: Sun, 7 Aug 2005 03:47:02 -0400 (EDT)
- References: <dd1itn$1et$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi, > Question: What is the difference between "bring" and String[bring] ? The first is an "atomic" string in Mathematica. The second is an expression With Head String and one "argument", the symbol bring. > What is the utility of making them different? You need to understand that there are a few atomic expressions in mathematica, among them Symbols, Strings and numbers like integers, reals and complex numbers. For theses the function Head returns the symbols Symbol, String, Integer, Real and Complex, but still they are not of the form headofexpr[something] like any nonatomic expression. Check this: AtomQ["bring"] AtomQ[String[bring]] This together with the in my opinion somewhat missleading default of the mathematica frontend to not show the enclosing "-characters for strings in outputcells gives rise to many missunderstandings. > "bring" ===String[bring] as explained, two very different things. > Cases[{"bring"}, _String] > > {bring} a list of one atomic string, correct answer > Cases[String[bring], _String] > > {} this gives an empty list because Cases[expr,form] only looks for pattern-matches in level one of expr (so has nothing to do with the String-confusion). Using the following: Cases[{String[bring]}, _String] Cases[String[bring], _String, {0, Infinity}, Heads -> True] will both return: {String[bring]} which is probably closer to what you might have expected, but _not_ the same as {"bring"} > Cases[ToString[bring], _String] > > {} same as above: Cases[{"bring"}, _String] Cases["bring", _String, {0, Infinity}, Heads -> True] will both return: {"bring"} which in the frontend is shown as just: {bring} so it might be a good idea to use InputForm or FullForm as in your later examples to see it really is {"bring"}: Cases[{"bring"}, _String]//InputForm Cases["bring", _String, {0, Infinity}, Heads -> True]//InputForm {"bring"} {"bring"} > InputForm[String[bring]] > > String[bring] > > InputForm["bring"] > > "bring" two different things as explained above. > FullForm[String[bring]] > > String[bring] > > And yet: > > Head["bring"] > > String you will have noticed that both of these give the same result: Head["bring"] Head[String[bring]] and this is the case for any atomic expression in Mathematica, not only strings and of course somewhat ambigous but unproblematic in everyday use, see for example: Head[1] Head[Integer[1]] > FullForm["bring"] > > "bring" > > ToString[bring] > > bring this is only because the frontend does not show the "-characters, using InputForm or FullForm on it yields the expected result > InputForm[%] > > "bring" I hope the above helps. You should also be aware that ToString is the obvious function to turn the symbol bring into the string "bring" but will fail as soon as you define an OwnValue for bring: bring=1; ToString[bring]//InputForm "1" the save way is to use SymbolName[bring]: bring=1; SymbolName[bring]//InputForm "bring" Maybe you want to set the Option ShowStringCharacters to true until you are more confident with what mathematica is doing with strings. This you can do with the option inspector: Edit->Preferences and then choose: Formatting Options -> Expression Formatting -> Display Options -> ShowStringCharacters and toggle this entry to true (you will notice that this effects all existing output cells without further evaluation, which is clearly showing that it really is an output-formatting issue only...). Then evaluating an InputCell containing: "bring" bring will return the easy to understand result: "bring" bring and not: bring bring as with the default settings... Albert