Post Reply 
prefix notation and () on newRPL project
04-20-2020, 02:01 AM (This post was last modified: 04-22-2020 08:43 PM by Albert Chan.)
Post: #8
RE: prefix notation and () on newRPL project
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)

(define (rpn-aux lst)
  (let scan ([e (cdr lst)] [x (car lst)] [s '()])
    (if (null? e)
      (if (null? s) x [cons* #'list x s]) ; return x or stack
        (case (syntax->datum (car e))
          ((+ - * / ^) (scan (cdr e) [list (car e) (car s) x] [cdr s]))
          ((@)         (scan (cdr e) [list x (car s)] [cdr s]))
          ((swap)      (scan (cdr e) [car s] [cons x (cdr s)]))
          ((dup)
            (if (atom? (syntax->datum x))
              (scan (cdr e) x [cons x s])
              (let ([dup (datum->syntax (car e) (gensym))])
                (list #'let (list (list dup x))
                  (scan (cdr e) dup [cons dup s])))))
          (else (scan (cdr e) [car e] [cons x s])))))) ; push data

(define-syntax (rpn stx)
  (syntax-case stx ()
    ((rpn x ...) (rpn-aux #'(x ...)))))

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
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: prefix notation and () on newRPL project - Albert Chan - 04-20-2020 02:01 AM



User(s) browsing this thread: 1 Guest(s)