Re: StringJoin with ToString
- To: mathgroup at smc.vnet.net
- Subject: [mg98360] Re: StringJoin with ToString
- From: replicatorzed at gmail.com
- Date: Wed, 8 Apr 2009 02:49:28 -0400 (EDT)
- References: <gr6cbs$pva$1@smc.vnet.net> <gra1qi$2jl$1@smc.vnet.net>
I was too hasty posting my reply: First, I'm extremely sorry to mix up your name, ADL. Second: this code of you does only half of the job: It can not convert lists to strings verbatim, that is toString[{1,2,3}] gives {"1","2","3"} instead of "{1,2,3}". Which (as it seems to me) is exactly the problem causing the wrong pinting of Messages. Still I wonder whether there is a reliable solution to apply ToString to inputs of StringJoin without explicitely writing it each time I call StringJoin. If I use a different name (e.g. ExtendedStringJoin, as it was suggested), I loose the effective shorthand version (<>), as it would still use the original function. Is there a way to overwrite at least the definitions of the infix operator? Istvan On Apr 5, 12:40 pm, ADL <alberto.dilu... at tiscali.it> wrote: > With the following definitions, your idea appears to work (at least in > a few examples I tested): > > ClearAll[toString]; > toString[x_?NumericQ, format___] := ToString[x, format]; > toString[x_[y__], format___] := x @@ (toString /@ {y}); > toString[x_Symbol, format___] := ToString[x, format]; > toString[x_, format___] := x; > > Unprotect[System`StringJoin]; > Unprotect[$UseNewStringJoin]; > $UseNewStringJoin = True; > System`StringJoin[expr___] := > Block[{$UseNewStringJoin = False}, > StringJoin@@ (toString /@ {expr})] /; TrueQ[$UseNewStringJoin]; > Protect[$UseNewStringJoin]; > Protect[System`StringJoin]; > > The problem was the way ToString works, which is incompatible with > intermediate formatting functions. The definition of "toString" does > the job. > > In[12]:=StringJoin[{1, 2, 3}] // InputForm > > Out[12]//InputForm= > "123" > > In[13]:=StringJoin[1, a, 3] // InputForm > > Out[13]//InputForm= > "1a3" > > In[17]:=StringJoin[1, "2", \[Pi]] // InputForm > > Out[17]//InputForm= > "12Pi" > > In[15]:= Append[dummy, 2] > > During evaluation of In[15]:= Append::normal: Nonatomic expression > expected at position 1 in Append[dummy,2]. >> > > Out[15]= Append[dummy,2] > > ADL > > On 4 Apr, 03:15, replicator... at gmail.com wrote: > > > It is quite interesting whyStringJoindoes not apply ToString to > > those arguments which are non-string. I've overridden the built-in > >StringJoin, to save a few unnecessary ToStrings by the standard method > > (the context specifications are necessary for packaging): > > ... > > > Thanks > > > Istvan Zachar