diff options
author | Franciszek Malinka <franciszek.malinka@gmail.com> | 2021-02-25 14:42:55 +0100 |
---|---|---|
committer | Franciszek Malinka <franciszek.malinka@gmail.com> | 2021-02-25 14:42:55 +0100 |
commit | 9477dbe667f250ecd23f8fc0d56b942191526421 (patch) | |
tree | a4b50c9a726f415f835f5311c11c5d66e95f688c /Semestr 2/racket/lista6/zad11 | |
parent | 1968c1e590077bd51844eacfac722d7963848cb8 (diff) |
Stare semestry, niepoukladane
Diffstat (limited to 'Semestr 2/racket/lista6/zad11')
-rw-r--r-- | Semestr 2/racket/lista6/zad11/solution.bak | 36 | ||||
-rw-r--r-- | Semestr 2/racket/lista6/zad11/solution.rkt | 58 |
2 files changed, 94 insertions, 0 deletions
diff --git a/Semestr 2/racket/lista6/zad11/solution.bak b/Semestr 2/racket/lista6/zad11/solution.bak new file mode 100644 index 0000000..f449481 --- /dev/null +++ b/Semestr 2/racket/lista6/zad11/solution.bak @@ -0,0 +1,36 @@ +#lang racket + +(provide (struct-out const) (struct-out binop) rpn->arith) + +;; ------------------------------- +;; Wyrazenia w odwr. not. polskiej +;; ------------------------------- + +(define (rpn-expr? e) + (and (list? e) + (pair? e) + (andmap (lambda (x) (or (number? x) (member x '(+ - * /)))) + e))) + +;; ---------------------- +;; Wyrazenia arytmetyczne +;; ---------------------- + +(struct const (val) #:transparent) +(struct binop (op l r) #:transparent) + +(define (arith-expr? e) + (match e + [(const n) (number? n)] + [(binop op l r) + (and (symbol? op) (arith-expr? l) (arith-expr? r))] + [_ false])) + +;; ---------- +;; Kompilacja +;; ---------- + +(define (rpn->arith e) + (error "TODO: Uzupelnij tutaj")) + +; Mozesz tez dodac jakies procedury pomocnicze i testy
\ No newline at end of file diff --git a/Semestr 2/racket/lista6/zad11/solution.rkt b/Semestr 2/racket/lista6/zad11/solution.rkt new file mode 100644 index 0000000..a44afe4 --- /dev/null +++ b/Semestr 2/racket/lista6/zad11/solution.rkt @@ -0,0 +1,58 @@ +#lang racket + +(provide (struct-out const) (struct-out binop) rpn->arith) + +;; ------------------------------- +;; Wyrazenia w odwr. not. polskiej +;; ------------------------------- + +(define (rpn-expr? e) + (and (list? e) + (pair? e) + (andmap (lambda (x) (or (number? x) (member x '(+ - * /)))) + e))) + +;; ---------------------- +;; Wyrazenia arytmetyczne +;; ---------------------- + +(struct const (val) #:transparent) +(struct binop (op l r) #:transparent) + +(define (arith-expr? e) + (match e + [(const n) (number? n)] + [(binop op l r) + (and (symbol? op) (arith-expr? l) (arith-expr? r))] + [_ false])) + +;; ---------- +;; Kompilacja +;; ---------- + +(struct stack (xs)) + +(define empty-stack (stack null)) +(define (empty-stack? s) (null? (stack-xs s))) +(define (top s) (car (stack-xs s))) +(define (push a s) (stack (cons a (stack-xs s)))) +(define (pop s) (stack (cdr (stack-xs s)))) + +(define (op->proc op) + (match op ['+ +] ['- -] ['* *] ['/ /])) + +(define (eval-am e s) + (cond [(null? e) + (top s)] + [(number? (car e)) + (eval-am (cdr e) (push (const (car e)) s))] + [(symbol? (car e)) + (eval-am (cdr e) + (push (binop (car e) (top (pop s)) (top s)) + (pop (pop s))))])) + +(define (rpn->arith e) + (eval-am e empty-stack)) + + +; Mozesz tez dodac jakies procedury pomocnicze i testy
\ No newline at end of file |