prefix notation and () on newRPL project - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: Not HP Calculators (/forum-7.html) +--- Forum: Not quite HP Calculators - but related (/forum-8.html) +--- Thread: prefix notation and () on newRPL project (/thread-12242.html) |
prefix notation and () on newRPL project - compsystems - 01-21-2019 05:06 PM Hi Claudio, 2 Crazy ideas =) ◉ Observing the u_LISP project that has a notation similar to the prefix notation, and that the R-PN is a inverse prefix. So I "think" it's easy to add a second type of operation engine PN/prefix ("eliminate" the Reverse) or consult it in this community if they are involved in this type of notation in your project. (3+4) * (5-6) returns -7 // "infix" operation or what better should be called standard notation *( +(3,4), -(5,6) ) prefix operation with ( ) * + 3 4 - 5 6 prefix operation ◉ A second idea, RPN with parentheses, this gives a faster interpretation of the source code 3 4 + 5 6 - * postfix operation ((3,4)+, (5,6)-)* postfix operation with () The importance of the use of parentheses is that each operator can operate on n arguments (a,b,c)+ (a,b,c,d,e)- RE: prefix notation and () on newRPL project - Claudio L. - 01-21-2019 05:51 PM (01-21-2019 05:06 PM)compsystems Wrote: Hi Claudio, While crazy, adding alternate syntax rules to RPL is possible and I'm considering an alternate language to the same code. The idea is to compile code in other languages to an RPL stream. Decompiling is much more complex so it would probably have to keep the source code separate (like most compiled languages). Lisp would be very straighforward: An opening parenthesis simply compiles as the start of a list '{', everything gets pushed to the stream until a closing parenthesis is found, which is compiled as an end of list '}'. Then parenthesis simply pushes the number of arguments, then does LIST-> ROLL EVAL to execute the first argument. But rather than adding a language of similar difficulty as RPL, the idea is to add a much higher level language, like Lua or python. Compiling these languages is very difficult, so I'm still studying this as a possibility, not saying I might do it. RE: prefix notation and () on newRPL project - Thomas Klemm - 01-21-2019 06:17 PM (01-21-2019 05:51 PM)Claudio L. Wrote: Lisp would be very straightforward … Not sure if you are aware of the power of Lisp macros. Cheers Thomas RE: prefix notation and () on newRPL project - Albert Chan - 01-21-2019 07:40 PM (01-21-2019 05:06 PM)compsystems Wrote: ◉ Observing the u_LISP project that has a notation similar to the prefix notation, and that the R-PN is a inverse prefix. Infix or rpn macro is not hard, but you are better off stay with lisp syntax. It does not take much time to get used to (* (+ 3 4) (- 5 6)) BTW, does u-lisp support macro ? Quote:◉ A second idea, RPN with parentheses, this gives a faster interpretation of the source code It might not run faster. I never tried u-lisp, but with Chez scheme 7.4d, (+ a b c) is slower than (+ (+ a b) c) Above assumed a,b,c are variables. With numbers, both form optimized to a single number. RE: prefix notation and () on newRPL project - Claudio L. - 01-21-2019 09:37 PM (01-21-2019 06:17 PM)Thomas Klemm Wrote: Not sure if you are aware of the power of Lisp macros. I wasn't. I did a lot of Lisp programming many years ago, but not on a general purpose Lisp. I used to code AutoLISP, the implementation used in AutoCAD. I have no idea if that implementation had macros (since AutoCAD has another syntax for macros called Diesel), and I never needed to use them anyway. But it looks really powerful. Unfortunately, that's not what I was talking about in this thread, implementing macros is a whole different animal than just doing the basic syntax over the RPL execution engine. RE: prefix notation and () on newRPL project - The Shadow - 01-22-2019 03:06 PM (01-21-2019 05:51 PM)Claudio L. Wrote: But rather than adding a language of similar difficulty as RPL, the idea is to add a much higher level language, like Lua or python. Python on my 50g would be quite incredible! List comprehension alone would make my day. Hmm. How hard would it be to do list comprehension in newRPL? I'm guessing very, but I honestly have no real idea. Syntax would be something like the bastard love-child of FOR/NEXT and SEQ. RE: prefix notation and () on newRPL project - Thomas Klemm - 01-22-2019 07:32 PM (01-22-2019 03:06 PM)The Shadow Wrote: List comprehension alone would make my day. What's wrong with SEQ? Python >>> [ x**2 + 1 for x in range(1, 11, 2) ] [2, 10, 26, 50, 82] Algebraic 'SQ(X)+1' 'X' 1 9 2 SEQ { 2 10 26 50 82 } Or using RPL « X SQ 1 + » 'X' 1 9 2 SEQ { 2 10 26 50 82 } Kind regards Thomas RE: prefix notation and () on newRPL project - Albert Chan - 04-20-2020 02:01 AM Here is a *fast* rpn macro for Chez Scheme Instead of using macro pattern matching, the code rearrange the list of syntax objects. Code: (define ^ expt) scheme> (load "rpn.ss") scheme> (define phi (rpn 5 sqrt @ 1 + 2 /)) scheme> (define (fib n) (rpn phi n ^ 1 phi - n ^ - 5 sqrt @ /)) scheme> (fib 10) 55.000000000000014 scheme> (fib 20) 6765.000000000005 RE: prefix notation and () on newRPL project - Albert Chan - 04-23-2020 05:06 PM 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 2024/11/4: 1. added unary +/- feature (next post) scheme> (calc-aux '(- 3 * + 4 ^ - 5)) (* (- 3) (+ (^ 4 (- 5)))) 2. added recursing into @ function arguments scheme> (calc (@ sqrt (5 *(((((1 + .2 *(350 / 661.5)^ 2)^ 3.5 - 1)* (1 - 6.875E-6 * 25500)^ - 5.2656)+ 1)^ .286 - 1)))) 0.8357245351752515 RE: prefix notation and () on newRPL project - Albert Chan - 12-23-2022 02:59 AM (12-23-2022 12:02 AM)Albert Chan Wrote: How to distinguish subtraction from negation Patched above (op +/-) logic to calc macro (previous post) Also, unary +/- precedence placed below POW, above MUL/DIV, similar to Python/Lua Before: Code: ((+ -) (if (null? (cdr e)) After: Code: ((+ -) (if (null? (cdr e)) Test with calc-expand macro (previous post) scheme> (calc-expand '(2 ^ - 3 ^ 4)) ; op +/- tests (^ 2 (- (^ 3 4))) scheme> (calc-expand '(2 * - 3 / 4)) (/ (* 2 (- 3)) 4) scheme> (calc-expand '(- 2 ^ 3 ^ 4)) ; unary +/- precedence below POW (- (^ 2 (^ 3 4))) scheme> (calc-expand '(- 2 * 3 / 4 )) ; unary +/- precedence above MUL/DIV (/ (* (- 2) 3) 4) And, for Mach number test. Spaces are needed to separate the tokens. scheme> (sqrt (calc 5 *(((((1 + .2 *(350 / 661.5)^ 2)^ 3.5 - 1)* (1 - 6.875E-6 * 25500)^ - 5.2656)+ 1)^ .286 - 1))) 0.8357245351752515 For comparison, we figured precedence rules, then use rpn macro. scheme> (rpn 350 661.5 / 2 ^ .2 * 1 + 3.5 ^ 1 - 1 6.875E-6 25500 * - -5.2656 ^ * 1 + .286 ^ 1 - 5 * sqrt @) 0.8357245351752515 RE: prefix notation and () on newRPL project - carey - 01-13-2023 04:15 PM (01-22-2019 07:32 PM)Thomas Klemm Wrote:(01-22-2019 03:06 PM)The Shadow Wrote: List comprehension alone would make my day. Thomas, thanks for this clear comparison! Just out of curiosity to see what this SEQ example would look like using the HP50g's not-often used algebraic mode (in contrast to the algebraic example in the quoted post that uses an algebraic expression in rpn mode), the following worked: Code:
{ 2 10 26 50 82 } Two things surprised me: (i) the algebraic mode parameter list for SEQ was intuitive so it was easy to guess right the first time, and although I'm reluctant to admit it, (ii) the function call looks nicer (to me) in algebraic mode than in RPL or Python. RE: prefix notation and () on newRPL project - FLISZT - 01-14-2023 12:29 AM (01-13-2023 04:15 PM)carey Wrote:(01-22-2019 07:32 PM)Thomas Klemm Wrote: What's wrong with SEQ? I agree with you. But fortunately, you can do the same thing in RPN mode. It's probably a little slower though. Just surround the entire expression with the character #96. I mean: << 96 CHR >> on your calculator or SHIFT↱ + CHARS ... then it's just the character before the lowercase "a" (unicode U+2035 on a computer). << ‵SEQ('SQ(X) + 1', 'X', 1, 9, 2)‵ >> RE: prefix notation and () on newRPL project - carey - 01-14-2023 01:20 AM (01-14-2023 12:29 AM)FLISZT Wrote: I agree with you. FLISZT, That's so cool that entire algebraic mode function calls (and not just algebraic expressions) can be used in rpn mode! Thank you, Carey |