4

I'm currently working on a math evaluator, which works by converting the infix notation to postfix notation and then evaluating the postfix expression. It was going pretty good, until I ran into the problem of unary operations and having functions accepting an infinite amount of parameters.

Say (infix notation): $$1+2*3$$

Which of course would be (postfix notation): $$1\enspace 2\enspace 3 * +$$

But how would we go about representing unary operations. Like $$4!$$ or $$-4$$ or $$not\enspace true$$

Further what about functions $sqrt(9)$ would be easy, as we know $sqrt$ only needs a single parameter. But what if the function accepts an arbitrary amount of parameters.

Thereby both (infix notation): $$f(1, 2) + 3$$ and $$1 * f(2) + 3$$

Would result in (postfix notation):

$$1\enspace 2\enspace f\enspace 3\enspace+$$

$$1\enspace 2\enspace f\enspace *\enspace 3\enspace +$$

My previous assumption here being that the function could just eat all the previous numbers, but that clearly isn't going to work...

vallentin
  • 207
  • Unary operators are just functions, so the answer for functions will satisfy it. For functions that take an arbitrary number of parameters, you will need a second symbol to indicate when you've reached the beginning of its parameters, so that everything before must be consumed by later operators. The same "wall" symbol can be used for all such functions, though, so you only need to introduce one. – Paul Sinclair Nov 05 '15 at 21:25
  • Use a stack. When you convert your operations you have to know the arity which means you'll need to distinguish between unary negation and binary subtraction. When you pop the operator, you then pop the appropriate number of parameters and push the answer back onto the stack. – John Douma Nov 05 '15 at 21:27
  • @PaulSinclair Even if the unary operator is on the right side of value? Also that makes sense, but I'm a bit confused of how that would "look". Could you give an example? – vallentin Nov 05 '15 at 21:28
  • You are displaying this postfix version rather than just using it internally? "$1\ -$" may look a little weird, but does it look any weirder than "$1\ 2\ -$"? As John points out, you need to distinguish between negation and subtraction, so you know if it is unary or binary. You can't use the same token for both like we do in writing. – Paul Sinclair Nov 05 '15 at 21:32
  • A different concept for variable parameter functions would be to simply add one more parameter to each of them. Make the last parameter before the function to be the count of parameters this particular instance of it takes. – Paul Sinclair Nov 05 '15 at 21:36
  • @PaulSinclair Ahh yes of course! I was actually already doing that for other stuff, don't know why I just thought subtraction and negation was the same... But for having a "parameter wall" symbol. You would/could do something like this, right? $1\enspace |\enspace 2\enspace f\enspace *\enspace 3\enspace +$ – vallentin Nov 05 '15 at 21:39

1 Answers1

4

One way to handle functions which take an arbitrary number of parameters is to introduce an additional "wall" token, such as "|". The wall would be entered to the left of the leftmost parameter of the function. When the function is encountered, it consumes all the parameters back to the wall. Later operators would consume any parameters before the wall. For example,

$$f(1,2) + 3 \longrightarrow |\ 1\ 2\ f\ 3\ +$$ $$1*f(2)+3 \longrightarrow 1\ |\ 2\ f\ *\ 3\ +$$ $$6 * (5 - f(4,3,2))+1 \longrightarrow 6\ 5\ |\ 4\ 3\ 2\ f\ -\ *\ 1\ +$$

If you don't want to denote unary negation by a different token than subtraction, then you could consider "$-$" to be a function that takes one or two parameters, and therefore needs a wall to differentiate:

$$1+2-(-3) \longrightarrow 1\ |\ 2\ |\ 3\ -\ -\ +$$


An alternate approach would be to preceed the function with an additional parameter, which would be the count of how many other parameters are consumed by the function:

$$f(1,2) + 3 \longrightarrow 1\ 2\ 2\ f\ 3\ +$$ $$1*f(2)+3 \longrightarrow 1\ 2\ 1\ f\ *\ 3\ +$$ $$6 * (5 - f(4,3,2))+1 \longrightarrow 6\ 5\ 4\ 3\ 2\ 3\ f\ -\ *\ 1\ +$$

$$1+2-(-3) \longrightarrow 1\ 2\ 3\ 1\ -\ 2\ -\ +$$

Paul Sinclair
  • 45,932
  • That is new to me. What groups or theories would I find wall operator? I am only an intermediate in mathematics. I'd like to learn wall operator. – Rita Geraghty Apr 17 '21 at 15:31
  • 2
    @RitaGeraghty - you can learn more about it by reading the comments on the OP above. Sorry, but both "count" and "wall" notation are things I invented to address this particular question. My answer here was not written to stand alone, but rather as part of the conversation in those comments. (I'd only been visiting this forum for a couple months when I gave this answer. Today I would have made the answer more self-contained.) – Paul Sinclair Apr 17 '21 at 15:58