Jump to content
  • Please help us to create a good repository of custom functions
    We realize there are other venus that you have posted to that store your brilliance.
    Take the opportunity to share it with the FM Forums Community.

    Thanks.

RPN ( expression )


Ocean West
  • Formulae:

    // RPN( expression )  or Reverse Polish Notation 
    // by Stephen Dolenski 
    
    Let ([
    exp = Substitute( expression ; " " ; ¶) ; // pivot expression into a return sep list.
    exp_len = ValueCount ( exp ) // count entries.
    ];
    
    
    //============================
    
    If(  $i   >   exp_len ;   
    
    Let( $i = 1 ; $stack)
    ;
    
    
    If(  Filter( GetValue(exp ; $i) ; "*+-/")  ≠ $null ;   // value is operator
    
         Let ([
              $result =  Evaluate(GetValue ($stack ; 1 )  &  GetValue(exp ; $i)  & GetValue ($stack ; 2 ))  ;
              $stack =  Substitute( $stack ; [GetValue ( $stack; 1 )  & "¶" & GetValue ( $stack; 2 )  & "¶"  ; $result &¶] ; [GetValue ( $stack; 1 )  & "¶" & GetValue ( $stack; 2 ) ; $result]);
              $i = $i+1
              ]; 
              RPN( exp ) 
              ) // end let
    
         ; // else append stack
    
            Let ([ 
            $stack = Case( $i = 1; GetValue (exp ; 1 ) ; GetValue(exp ; $i) & ¶& $stack );
            $i = $i+1
            ]; RPN( exp )
            ) // end let
         ) // end if
    
    ) // end if
    
    
    //============================
    
    ) // close let
    

This CF will allow you to evaluate an expression with Rerverse Polish Notation. Where entry detrmines order of operation, using a stack to push and pull from.

RPN input would be written : 10 25 2 * + 40 +
Infix input would be written : ((25 * 2) + 10) + 40

see also http://en.wikipedia.org/wiki/Reverse_polish

Edited by Ocean West

  • Like 1
  Report Function


User Feedback


cliftz

Posted · Report

reminds me of when I used to 'program' my HP48GX. Started with RPL.

Share this comment


Link to comment
Share on other sites


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

By using this site, you agree to our Terms of Use.