Re: Special Input Forms

*To*: mathgroup at yoda.physics.unc.edu*Subject*: Re: Special Input Forms*From*: withoff (David Withoff)*Date*: Fri, 14 Jan 1994 13:15:18 -0600

> Does anyone know how to make shorthand input forms in Mathematica? > [deleted] > Specifically, I would like an expression like the > following: > > 2 +/- 3 > > to evaluate to > > Uncertainty[2,3] > --------- > You're talking about adding new shorthands to the MMA parser. > To the best of my knowledge, this is not possible at the input > prompt level. The best you might do is to play games with > object rules, for instance, by defining +/-3 as the 'object' > UncertainInteger[3], and then making a rule so that > > 2 + UncertainInteger[3] > > reduces to > > Uncertainty[2,3] > > (similarly, 2 - UncertainInteger[3] would give the same answer). > > If you *really* want to be able to use the syntax 2 +/- 3, you're > going to have to write your own parser (either external to MMA, > or through an MMA function like MyParser[s_String]) that would > catch the '+/-' cases and convert them to Uncertainty function > calls. Given the complexity of the precedence rules for MMA's > language syntax, I think this will be a waste of effort. > > > \\|// "An intellectual carrot... > - - The mind boggles!!" > o o -- The Thing (1951) > J roberto sierra > O tempered microdesigns NOTICE: > \_/ san francisco, ca The ideas and opinions expressed > bert at netcom.com herein are not those of the author. It is possible to handle this particular example using $PreRead, and although the given specification of the problem is incomplete, it may be possible to do the entire problem this way. In[10]:= $PreRead = (StringReplace[#, "+/-" -> "~Uncertainty~"] &) ; In[11]:= 2 +/- 3 Out[11]= Uncertainty[2, 3] As can be inferred from the appendix of the Mathematica Book (under Special Input Forms, appendix A.2.3, page 716), this will effectively introduce an infix operator +/- which is left-associative In[13]:= x +/- y +/- z Out[13]= Uncertainty[Uncertainty[x, y], z] and which has a precedence between that of ++ and + In[14]:= a ++ +/- b + c Out[14]= c + Uncertainty[a++, b] There are lots of other things to try between this and writing your own parser, and even the latter it isn't an insurmountable task. The aforementioned appendix gives nearly all of the Mathematica-specific information (tokens, precedence, and associativity) needed to build a parser. Parsing and lexical analysis is being restructured a bit for Version 2.3 in a way that will make this sort of thing even easier. Dave Withoff Research and Development Wolfram Research