aboutsummaryrefslogtreecommitdiff
path: root/semestr-2/racket/lista6/solution.bak
diff options
context:
space:
mode:
Diffstat (limited to 'semestr-2/racket/lista6/solution.bak')
-rw-r--r--semestr-2/racket/lista6/solution.bak27
1 files changed, 27 insertions, 0 deletions
diff --git a/semestr-2/racket/lista6/solution.bak b/semestr-2/racket/lista6/solution.bak
new file mode 100644
index 0000000..0805991
--- /dev/null
+++ b/semestr-2/racket/lista6/solution.bak
@@ -0,0 +1,27 @@
+#lang racket
+
+(provide (struct-out complex) parse eval)
+
+(struct complex (re im) #:transparent)
+
+(define value?
+ complex?)
+
+;; Ponizej znajduje sie interpreter zwyklych wyrazen arytmetycznych.
+;; Zadanie to zmodyfikowac go tak, by dzialal z liczbami zespolonymi.
+
+(struct const (val) #:transparent)
+(struct binop (op l r) #:transparent)
+
+(define (op->proc op)
+ (match op ['+ +] ['- -] ['* *] ['/ /]))
+
+(define (eval e)
+ (match e
+ [(const n) n]
+ [(binop op l r) ((op->proc op) (eval l) (eval r))]))
+
+(define (parse q)
+ (cond [(number? q) (const q)]
+ [(and (list? q) (eq? (length q) 3) (symbol? (first q)))
+ (binop (first q) (parse (second q)) (parse (third q)))])) \ No newline at end of file