prefix notation and () on newRPL project
|
04-23-2020, 05:06 PM
(This post was last modified: 04-29-2020 09:35 PM by Albert Chan.)
Post: #9
|
|||
|
|||
RE: prefix notation and () on newRPL project
For completeness, this is my attempt for infix-to-prefix calc macro, for Chez Scheme
Note: the code scan tokens in reverse, from right to left. This simplified code to use stack only for (* /) (calc whatever +/- x) ⇒ (+/- (calc whatever) x) ; unary ± has higher precedence than infix ± (calc whatever x ^ y) ⇒ (calc whatever (^ x y)) ; ^ has associativity right to left Code: (define ^ expt) scheme> (load "calc.ss") scheme> (calc - 2 ^ 3 ^ 4) ; = - (2 ^ (3 ^ 4)) -2417851639229258349412352 scheme> (calc - (2 ^ 3) ^ 4) -4096 scheme> (calc 1 + 2 3 4) ; implied multiply 25 scheme> (define (calc-expand s) (syntax->datum (calc-aux (syntax->list s)))) scheme> (calc-expand #'(1 + 2 3 4)) (+ 1 (* (* 2 3) 4)) scheme> (define phi (calc (1 + (@ sqrt 5)) / 2)) scheme> (define (fib n) (calc (phi ^ n - (1 - phi) ^ n) / (@ sqrt 5))) scheme> (fib 10) 55.000000000000014 scheme> (fib 20) 6765.000000000005 calc macro had unary (+ -) between binary (* /) and (+ -), similar to Fortran 77 see http://macnauchtan.com/pub/precedence.html#_Fortran |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)