Thread: RPN & Functions

  1. #1
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902

    RPN & Functions

    I wrote a program a while back to take an Infix expression ("2 + 2 * 3") and convert it to Postfix notation (Reverse Polish) and evaluate it. Worked like a charm, and can handle things as complex as "2 * (4 + 5 / 5 * sin(490.333))"
    Now what I'm wondering is if there is any way to implement functions with a variable number of arguments. The only bright ideas I've had so far is "don't" and to keep the first thing on the stack as a "# of parameters", for those functions that need them. So, say log could take either 1 or 2 parameters, and took the form log(number, [base]), where base is the optional parameter. So far, I suppose I could write:
    number 1 log
    or
    base number 2 log
    But keeping the # of arguments like that seems like a kludge, especially if I ever want to actually use the RPN side as a human, and not just for evaluating the results of a infix conversion.

    Any other ideas on how to implement this? Are there any RPN calculators out there with variable-number of argument functions - if so, how do they do it?
    (Reverse Polish Notation (RPN), or postfix notation)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The function could perhaps just pop off as many arguments as it needs. If the function doesn't know how many parameters it needs, you have no choice but passing the number of arguments anyway, or perhaps an end-of-arguments sentinel.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I'm thinking that it should be the parser which reads either
    log(10)
    or
    log(10,2)
    and always pushes the default value onto the stack if none is specified.

    Then when the evaluate comes along, the correct number of parameters are always present.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But I think he's referring to functions that can take any amount of parameters, like
    Code:
    function max(parameter_bundle numbers)
      result = first of numbers
      for each n in numbers except first do
        if n greater than result then result = n
      end
    end with result
    Called like
    max(1, 2, 3, 4, 5, 6, 7, 8) -> 8
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  2. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  3. Question regarding RPN and error detection
    By Roule in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2004, 01:12 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM