Re: new special forms
- To: mathgroup at yoda.physics.unc.edu
- Subject: Re: new special forms
- From: withoff (David Withoff)
- Date: Mon, 9 May 1994 13:16:55 -0500
> Mathematica provides ~ for using a function in infix form. However, > I would like a way to make my own special forms that don't need ~. > For example, I'd like 3_-5 to mean 3 10^-5. In the quest for a way > to enter numbers in scientific notation more easily, I came up with > this kludge: > > Unprotect[StringJoin] > StringJoin[m_?NumberQ,e_?NumberQ] := m 10^e > Protect[StringJoin] > > Then 3<>-5 gives 3 10^-5, and does not interfere with the normal > operation of <>. This has two drawbacks: a) it's a little cumbersome > to type and so is only marginally better than blank 10^, and b) the > precedence is high, but not quite high enough, since something like > (#^2)& @ 3<>-5 gives 9 10^-5 instead of 9 10^-10. > > So, the questions are: is there any way to define my own special form > with my own specified precedence and operation? Is there a way to > redefine a higher precedence form like ? or :: (I couldn't find a > way)? Is there some other completely different approach to this > problem? Thanks. > > mark The only general way to do this is to write your own parser, which is certainly a possibility, although it is probably more work than you had in mind. The current Mathematica parser is not programmable. The closest available in the current version of Mathematica is $PreRead, which you could set to a rule that replaces the characters you enter with the corresponding characters expected by the parser. Something like $PreRead = StringReplace[#, {"0_" -> "0*10^", "1_" -> "1*10^", "2_" -> "2*10^", "3_" -> "3*10^", "4_" -> "4*10^", "5_" -> "5*10^", "6_" -> "6*10^", "7_" -> "7*10^", "8_" -> "8*10^", "9_" -> "9*10^"}] & In[2]:= 3_x x Out[2]= 3 10 In[3]:= 3_-5 3 Out[3]= ------ 100000 has many obvious limitations, but will cover generic cases such as the one you mentioned. Writing a more comprehensive rule would amount to writing the better part of a parser. Dave Withoff Research and Development Wolfram Research