From c5fcf7179a83ef65c86c6a4a390029149e518649 Mon Sep 17 00:00:00 2001 From: Franciszek Malinka Date: Tue, 5 Oct 2021 21:49:54 +0200 Subject: Duzy commit ze smieciami --- semestr-2/racket/lista6/lista8/kappa.py | 13 +++ semestr-2/racket/lista6/lista8/zad1.bak | 98 +++++++++++++++++ semestr-2/racket/lista6/lista8/zad1.rkt | 104 ++++++++++++++++++ semestr-2/racket/lista6/lista8/zad4.bak | 114 +++++++++++++++++++ semestr-2/racket/lista6/lista8/zad4.rkt | 118 ++++++++++++++++++++ semestr-2/racket/lista6/lista8/zad5.bak | 1 + semestr-2/racket/lista6/lista8/zad5.rkt | 151 +++++++++++++++++++++++++ semestr-2/racket/lista6/lista8/zad6.bak | 151 +++++++++++++++++++++++++ semestr-2/racket/lista6/lista8/zad6.rkt | 171 +++++++++++++++++++++++++++++ semestr-2/racket/lista6/lista8/zadanie.rkt | 98 +++++++++++++++++ semestr-2/racket/lista6/solution.bak | 27 +++++ semestr-2/racket/lista6/solution.rkt | 73 ++++++++++++ semestr-2/racket/lista6/zad11/solution.bak | 36 ++++++ semestr-2/racket/lista6/zad11/solution.rkt | 58 ++++++++++ 14 files changed, 1213 insertions(+) create mode 100644 semestr-2/racket/lista6/lista8/kappa.py create mode 100644 semestr-2/racket/lista6/lista8/zad1.bak create mode 100644 semestr-2/racket/lista6/lista8/zad1.rkt create mode 100644 semestr-2/racket/lista6/lista8/zad4.bak create mode 100644 semestr-2/racket/lista6/lista8/zad4.rkt create mode 100644 semestr-2/racket/lista6/lista8/zad5.bak create mode 100644 semestr-2/racket/lista6/lista8/zad5.rkt create mode 100644 semestr-2/racket/lista6/lista8/zad6.bak create mode 100644 semestr-2/racket/lista6/lista8/zad6.rkt create mode 100644 semestr-2/racket/lista6/lista8/zadanie.rkt create mode 100644 semestr-2/racket/lista6/solution.bak create mode 100644 semestr-2/racket/lista6/solution.rkt create mode 100644 semestr-2/racket/lista6/zad11/solution.bak create mode 100644 semestr-2/racket/lista6/zad11/solution.rkt (limited to 'semestr-2/racket/lista6') diff --git a/semestr-2/racket/lista6/lista8/kappa.py b/semestr-2/racket/lista6/lista8/kappa.py new file mode 100644 index 0000000..f359d5c --- /dev/null +++ b/semestr-2/racket/lista6/lista8/kappa.py @@ -0,0 +1,13 @@ +import pylab + +a = int(input("podaj liczbe: ")) +b = int(input("podaj liczbe: ")) + +x = range(-10, 11) +y = [] +for i in x: + y.append(a * i + b) +pylab.plot(x, y) +pylab.title('Wykres f(x) = a*x - b') +pylab.grid(True) +pylab.show() diff --git a/semestr-2/racket/lista6/lista8/zad1.bak b/semestr-2/racket/lista6/lista8/zad1.bak new file mode 100644 index 0000000..0960f21 --- /dev/null +++ b/semestr-2/racket/lista6/lista8/zad1.bak @@ -0,0 +1,98 @@ +#lang racket + +; Do let-env.rkt dodajemy wartosci boolowskie +; +; Miejsca, ktore sie zmienily oznaczone sa przez !!! + +; --------- ; +; Wyrazenia ; +; --------- ; + +(struct const (val) #:transparent) +(struct binop (op l r) #:transparent) +(struct var-expr (id) #:transparent) +(struct let-expr (id e1 e2) #:transparent) +(struct if-expr (eb et ef) #:transparent) + +(define (expr? e) + (match e + [(const n) (or (number? n) (boolean? n))] ; <----------------- !!! + [(binop op l r) (and (symbol? op) (expr? l) (expr? r))] + [(var-expr x) (symbol? x)] + [(let-expr x e1 e2) + (and (symbol? x) (expr? e1) (expr? e2))] + [(if-expr eb et ef) ; <--------------------------------------- !!! + (and (expr? eb) (expr? et) (expr? ef))] + [_ false])) + +(define (parse q) + (cond + [(number? q) (const q)] + [(eq? q 'true) (const true)] ; <---------------------------- !!! + [(eq? q 'false) (const false)] ; <---------------------------- !!! + [(symbol? q) (var-expr q)] + [(and (list? q) (eq? (length q) 3) (eq? (first q) 'let)) + (let-expr (first (second q)) + (parse (second (second q))) + (parse (third q)))] + [(and (list? q) (eq? (length q) 4) (eq? (first q) 'if)) ; <--- !!! + (if-expr (parse (second q)) + (parse (third q)) + (parse (fourth q)))] + [(and (list? q) (eq? (length q) 3) (symbol? (first q))) + (binop (first q) + (parse (second q)) + (parse (third q)))])) + +(define (test-parse) (parse '(let [x (+ 2 2)] (+ x 1)))) + +; ---------- ; +; Srodowiska ; +; ---------- ; + +(struct environ (xs)) + +(define env-empty (environ null)) +(define (env-add x v env) + (environ (cons (cons x v) (environ-xs env)))) +(define (env-lookup x env) + (define (assoc-lookup xs) + (cond [(null? xs) (error "Unknown identifier" x)] + [(eq? x (car (car xs))) (cdr (car xs))] + [else (assoc-lookup (cdr xs))])) + (assoc-lookup (environ-xs env))) + +; --------- ; +; Ewaluacja ; +; --------- ; + +(define (value? v) + (or (number? v) (boolean? v))) + +(define (op->proc op) + (match op ['+ +] ['- -] ['* *] ['/ /] ['% modulo] ; <----------- !!! + ['= =] ['> >] ['>= >=] ['< <] ['<= <=] + ['and (lambda (x y) (and x y))] + ['or (lambda (x y) (or x y))])) + +(define (eval-env e env) + (match e + [(const n) n] + [(binop op l r) ((op->proc op) (eval-env l env) + (eval-env r env))] + [(let-expr x e1 e2) + (eval-env e2 (env-add x (eval-env e1 env) env))] + [(var-expr x) (env-lookup x env)] + [(if-expr eb et ef) (if (eval-env eb env) ; <----------------- !!! + (eval-env et env) + (eval-env ef env))])) + +(define (eval e) (eval-env e env-empty)) + +(define program + '(if (or (< (% 123 10) 5) + true) + (+ 2 3) + (/ 2 0))) + +(define (test-eval) (eval (parse program))) \ No newline at end of file diff --git a/semestr-2/racket/lista6/lista8/zad1.rkt b/semestr-2/racket/lista6/lista8/zad1.rkt new file mode 100644 index 0000000..1cd6b0b --- /dev/null +++ b/semestr-2/racket/lista6/lista8/zad1.rkt @@ -0,0 +1,104 @@ +#lang racket + +; Do let-env.rkt dodajemy wartosci boolowskie +; +; Miejsca, ktore sie zmienily oznaczone sa przez !!! + +; --------- ; +; Wyrazenia ; +; --------- ; + +(struct const (val) #:transparent) +(struct binop (op l r) #:transparent) +(struct var-expr (id) #:transparent) +(struct let-expr (id e1 e2) #:transparent) +(struct if-expr (eb et ef) #:transparent) + +(define (expr? e) + (match e + [(const n) (or (number? n) (boolean? n))] ; <----------------- !!! + [(binop op l r) (and (symbol? op) (expr? l) (expr? r))] + [(var-expr x) (symbol? x)] + [(let-expr x e1 e2) + (and (symbol? x) (expr? e1) (expr? e2))] + [(if-expr eb et ef) ; <--------------------------------------- !!! + (and (expr? eb) (expr? et) (expr? ef))] + [_ false])) + +(define (parse q) + (cond + [(number? q) (const q)] + [(eq? q 'true) (const true)] ; <---------------------------- !!! + [(eq? q 'false) (const false)] ; <---------------------------- !!! + [(symbol? q) (var-expr q)] + [(and (list? q) (eq? (length q) 3) (eq? (first q) 'let)) + (let-expr (first (second q)) + (parse (second (second q))) + (parse (third q)))] + [(and (list? q) (eq? (length q) 4) (eq? (first q) 'if)) ; <--- !!! + (if-expr (parse (second q)) + (parse (third q)) + (parse (fourth q)))] + [(and (list? q) (eq? (length q) 3) (eq? (first q) 'and)) + (if-expr (parse (second q)) + (parse (third q)) + (const false))] + [(and (list? q) (eq? (length q) 3) (eq? (first q) 'or)) + (if-expr (parse (second q)) + (const true) + (parse (third q)))] + [(and (list? q) (eq? (length q) 3) (symbol? (first q))) + (binop (first q) + (parse (second q)) + (parse (third q)))])) + +(define (test-parse) (parse '(let [x (+ 2 2)] (+ x 1)))) + +; ---------- ; +; Srodowiska ; +; ---------- ; + +(struct environ (xs)) + +(define env-empty (environ null)) +(define (env-add x v env) + (environ (cons (cons x v) (environ-xs env)))) +(define (env-lookup x env) + (define (assoc-lookup xs) + (cond [(null? xs) (error "Unknown identifier" x)] + [(eq? x (car (car xs))) (cdr (car xs))] + [else (assoc-lookup (cdr xs))])) + (assoc-lookup (environ-xs env))) + +; --------- ; +; Ewaluacja ; +; --------- ; + +(define (value? v) + (or (number? v) (boolean? v))) + +(define (op->proc op) + (match op ['+ +] ['- -] ['* *] ['/ /] ['% modulo] ; <----------- !!! + ['= =] ['> >] ['>= >=] ['< <] ['<= <=])) + +(define (eval-env e env) + (match e + [(const n) n] + [(binop op l r) ((op->proc op) (eval-env l env) + (eval-env r env))] + [(let-expr x e1 e2) + (eval-env e2 (env-add x (eval-env e1 env) env))] + [(var-expr x) (env-lookup x env)] + [(if-expr eb et ef) (if (eval-env eb env) ; <----------------- !!! + (eval-env et env) + (eval-env ef env))])) + +(define (eval e) (eval-env e env-empty)) + +(define program + '(if (or (< (% 123 10) 5) + true) + (+ 2 3) + (/ 2 0))) + +(define (test-eval) (eval (parse program))) \ No newline at end of file diff --git a/semestr-2/racket/lista6/lista8/zad4.bak b/semestr-2/racket/lista6/lista8/zad4.bak new file mode 100644 index 0000000..503099d --- /dev/null +++ b/semestr-2/racket/lista6/lista8/zad4.bak @@ -0,0 +1,114 @@ +#lang racket + +; Do boolean.rkt dodajemy pary +; +; Miejsca, ktore sie zmienily oznaczone sa przez !!! + +; --------- ; +; Wyrazenia ; +; --------- ; + +(struct const (val) #:transparent) +(struct binop (op l r) #:transparent) +(struct var-expr (id) #:transparent) +(struct let-expr (id e1 e2) #:transparent) +(struct if-expr (eb et ef) #:transparent) +(struct cons-expr (e1 e2) #:transparent) ; <------------------- !!! +(struct car-expr (e) #:transparent) ; <------------------- !!! +(struct cdr-expr (e) #:transparent) ; <------------------- !!! + +(define (expr? e) + (match e + [(const n) (or (number? n) (boolean? n))] + [(binop op l r) (and (symbol? op) (expr? l) (expr? r))] + [(var-expr x) (symbol? x)] + [(let-expr x e1 e2) + (and (symbol? x) (expr? e1) (expr? e2))] + [(if-expr eb et ef) + (and (expr? eb) (expr? et) (expr? ef))] + [(cons-expr e1 e2) (and (expr? e1) (expr? e2))] ; <----------- !!! + [(car-expr e) (expr? e)] ; <---------------------------------- !!! + [(cdr-expr e) (expr? e)] ; <---------------------------------- !!! + [_ false])) + +(define (parse q) + (cond + [(number? q) (const q)] + [(eq? q 'true) (const true)] + [(eq? q 'false) (const false)] + [(symbol? q) (var-expr q)] + [(and (list? q) (eq? (length q) 3) (eq? (first q) 'cons)) ; <- !!! + (cons-expr (parse (second q)) + (parse (third q)))] + [(and (list? q) (eq? (length q) 2) (eq? (first q) 'car)) ; <-- !!! + (car-expr (parse (second q)))] + [(and (list? q) (eq? (length q) 2) (eq? (first q) 'cdr)) ; <-- !!! + (cdr-expr (parse (second q)))] + [(and (list? q) (eq? (length q) 3) (eq? (first q) 'let)) + (let-expr (first (second q)) + (parse (second (second q))) + (parse (third q)))] + [(and (list? q) (eq? (length q) 4) (eq? (first q) 'if)) + (if-expr (parse (second q)) + (parse (third q)) + (parse (fourth q)))] + [(and (list? q) (eq? (length q) 3) (symbol? (first q))) + (binop (first q) + (parse (second q)) + (parse (third q)))])) + +(define (test-parse) (parse '(let [x (+ 2 2)] (+ x 1)))) + +; ---------- ; +; Srodowiska ; +; ---------- ; + +(struct environ (xs)) + +(define env-empty (environ null)) +(define (env-add x v env) + (environ (cons (cons x v) (environ-xs env)))) +(define (env-lookup x env) + (define (assoc-lookup xs) + (cond [(null? xs) (error "Unknown identifier" x)] + [(eq? x (car (car xs))) (cdr (car xs))] + [else (assoc-lookup (cdr xs))])) + (assoc-lookup (environ-xs env))) + +; --------- ; +; Ewaluacja ; +; --------- ; + +(define (value? v) + (or (number? v) + (boolean? v) + (and (pair? v) (value? (car v)) (value? (cdr v))))) + +(define (op->proc op) + (match op ['+ +] ['- -] ['* *] ['/ /] ['% modulo] + ['= =] ['> >] ['>= >=] ['< <] ['<= <=] + ['and (lambda (x y) (and x y))] + ['or (lambda (x y) (or x y))])) + +(define (eval-env e env) + (match e + [(const n) n] + [(binop op l r) ((op->proc op) (eval-env l env) + (eval-env r env))] + [(let-expr x e1 e2) + (eval-env e2 (env-add x (eval-env e1 env) env))] + [(var-expr x) (env-lookup x env)] + [(if-expr eb et ef) (if (eval-env eb env) + (eval-env et env) + (eval-env ef env))] + [(cons-expr e1 e2) (cons (eval-env e1 env) ; <---------------- !!! + (eval-env e2 env))] + [(car-expr e) (car (eval-env e env))] ; <--------------------- !!! + [(cdr-expr e) (cdr (eval-env e env))])) ; <------------------- !!! + +(define (eval e) (eval-env e env-empty)) + +(define program + '(car (if true (cons 1 2) false))) + +(define (test-eval) (eval (parse program))) \ No newline at end of file diff --git a/semestr-2/racket/lista6/lista8/zad4.rkt b/semestr-2/racket/lista6/lista8/zad4.rkt new file mode 100644 index 0000000..7934435 --- /dev/null +++ b/semestr-2/racket/lista6/lista8/zad4.rkt @@ -0,0 +1,118 @@ +#lang racket + +; Do boolean.rkt dodajemy pary +; +; Miejsca, ktore sie zmienily oznaczone sa przez !!! + +; --------- ; +; Wyrazenia ; +; --------- ; + +(struct const (val) #:transparent) +(struct binop (op l r) #:transparent) +(struct var-expr (id) #:transparent) +(struct let-expr (id e1 e2) #:transparent) +(struct if-expr (eb et ef) #:transparent) +(struct cons-expr (e1 e2) #:transparent) ; <------------------- !!! +(struct car-expr (e) #:transparent) ; <------------------- !!! +(struct cdr-expr (e) #:transparent) ; <------------------- !!! +(struct is-pair (e) #:transparent) + +(define (expr? e) + (match e + [(const n) (or (number? n) (boolean? n))] + [(binop op l r) (and (symbol? op) (expr? l) (expr? r))] + [(var-expr x) (symbol? x)] + [(let-expr x e1 e2) + (and (symbol? x) (expr? e1) (expr? e2))] + [(if-expr eb et ef) + (and (expr? eb) (expr? et) (expr? ef))] + [(cons-expr e1 e2) (and (expr? e1) (expr? e2))] ; <----------- !!! + [(car-expr e) (expr? e)] ; <---------------------------------- !!! + [(cdr-expr e) (expr? e)] ; <---------------------------------- !!! + [(is-pair e) (expr? e)] + [_ false])) + +(define (parse q) + (cond + [(number? q) (const q)] + [(eq? q 'true) (const true)] + [(eq? q 'false) (const false)] + [(symbol? q) (var-expr q)] + [(and (list? q) (eq? (length q) 3) (eq? (first q) 'cons)) ; <- !!! + (cons-expr (parse (second q)) + (parse (third q)))] + [(and (list? q) (eq? (length q) 2) (eq? (first q) 'car)) ; <-- !!! + (car-expr (parse (second q)))] + [(and (list? q) (eq? (length q) 2) (eq? (first q) 'cdr)) ; <-- !!! + (cdr-expr (parse (second q)))] + [(and (list? q) (eq? (length q) 3) (eq? (first q) 'let)) + (let-expr (first (second q)) + (parse (second (second q))) + (parse (third q)))] + [(and (list? q) (eq? (length q) 4) (eq? (first q) 'if)) + (if-expr (parse (second q)) + (parse (third q)) + (parse (fourth q)))] + [(and (list? q) (eq? (length q) 3) (symbol? (first q))) + (binop (first q) + (parse (second q)) + (parse (third q)))] + [(and (list? q) (eq? (length q) 2) (eq? (first q) 'pair?)) + (is-pair (parse (second q)))])) + +(define (test-parse) (parse '(let [x (+ 2 2)] (+ x 1)))) + +; ---------- ; +; Srodowiska ; +; ---------- ; + +(struct environ (xs)) + +(define env-empty (environ null)) +(define (env-add x v env) + (environ (cons (cons x v) (environ-xs env)))) +(define (env-lookup x env) + (define (assoc-lookup xs) + (cond [(null? xs) (error "Unknown identifier" x)] + [(eq? x (car (car xs))) (cdr (car xs))] + [else (assoc-lookup (cdr xs))])) + (assoc-lookup (environ-xs env))) + +; --------- ; +; Ewaluacja ; +; --------- ; + +(define (value? v) + (or (number? v) + (boolean? v) + (and (pair? v) (value? (car v)) (value? (cdr v))))) + +(define (op->proc op) + (match op ['+ +] ['- -] ['* *] ['/ /] ['% modulo] + ['= =] ['> >] ['>= >=] ['< <] ['<= <=] + ['and (lambda (x y) (and x y))] + ['or (lambda (x y) (or x y))])) + +(define (eval-env e env) + (match e + [(const n) n] + [(binop op l r) ((op->proc op) (eval-env l env) + (eval-env r env))] + [(let-expr x e1 e2) + (eval-env e2 (env-add x (eval-env e1 env) env))] + [(var-expr x) (env-lookup x env)] + [(if-expr eb et ef) (if (eval-env eb env) + (eval-env et env) + (eval-env ef env))] + [(cons-expr e1 e2) (cons (eval-env e1 env) ; <---------------- !!! + (eval-env e2 env))] + [(car-expr e) (car (eval-env e env))] ; <--------------------- !!! + [(cdr-expr e) (cdr (eval-env e env))] ; <------------------- !!! + [(is-pair e) (cons? (eval-env e env))])) +(define (eval e) (eval-env e env-empty)) + +(define program + '(car (if true (cons 1 2) false))) + +(define (test-eval) (eval (parse program))) \ No newline at end of file diff --git a/semestr-2/racket/lista6/lista8/zad5.bak b/semestr-2/racket/lista6/lista8/zad5.bak new file mode 100644 index 0000000..6f1f7b4 --- /dev/null +++ b/semestr-2/racket/lista6/lista8/zad5.bak @@ -0,0 +1 @@ +#lang racket diff --git a/semestr-2/racket/lista6/lista8/zad5.rkt b/semestr-2/racket/lista6/lista8/zad5.rkt new file mode 100644 index 0000000..721f5bf --- /dev/null +++ b/semestr-2/racket/lista6/lista8/zad5.rkt @@ -0,0 +1,151 @@ +#lang racket + +; Do list.rkt dodajemy procedury +; +; Miejsca, ktore sie zmienily oznaczone sa przez !!! + +; --------- ; +; Wyrazenia ; +; --------- ; + +(struct const (val) #:transparent) +(struct binop (op l r) #:transparent) +(struct unop (op e) #:transparent) +(struct var-expr (id) #:transparent) +(struct let-expr (id e1 e2) #:transparent) +(struct if-expr (eb et ef) #:transparent) +(struct cons-expr (e1 e2) #:transparent) +(struct null-expr () #:transparent) +(struct null?-expr (e) #:transparent) +(struct app (f e) #:transparent) ; <------------------ !!! +(struct lam (id e) #:transparent) ; <------------------ !!! + + +(define (expr? e) + (match e + [(const n) (or (number? n) (boolean? n))] + [(binop op l r) (and (symbol? op) (expr? l) (expr? r))] + [(var-expr x) (symbol? x)] + [(let-expr x e1 e2) + (and (symbol? x) (expr? e1) (expr? e2))] + [(if-expr eb et ef) + (and (expr? eb) (expr? et) (expr? ef))] + [(cons-expr e1 e2) (and (expr? e1) (expr? e2))] + [(null-expr) true] + [(null?-expr e) (expr? e)] + [(app f e) (and (expr? f) (expr? e))] ; <--------------------- !!! + [(lam id e) (and (symbol? id) (expr? e))] ; <----------------- !!! + [_ false])) + +(define (parse q) + (cond + [(number? q) (const q)] + [(eq? q 'true) (const true)] + [(eq? q 'false) (const false)] + [(eq? q 'null) (null-expr)] + [(symbol? q) (var-expr q)] + [(and (list? q) (eq? (length q) 2) (eq? (first q) 'null?)) + (null?-expr (parse (second q)))] + [(and (list? q) (eq? (length q) 3) (eq? (first q) 'cons)) + (cons-expr (parse (second q)) + (parse (third q)))] + [(and (list? q) (eq? (length q) 3) (eq? (first q) 'let)) + (let-expr (first (second q)) + (parse (second (second q))) + (parse (third q)))] + [(and (list? q) (eq? (length q) 4) (eq? (first q) 'if)) + (if-expr (parse (second q)) + (parse (third q)) + (parse (fourth q)))] + [(and (list? q) (eq? (length q) 3) (eq? (first q) 'lambda)) ; proc (car q)))) ; <------- !!! + (parse-app q)] + [(and (list? q) (eq? (length q) 3) (symbol? (first q))) + (binop (first q) + (parse (second q)) + (parse (third q)))] + [(and (list? q) (eq? (length q) 2) (symbol? (first q))) + (unop (first q) (parse (second q)))])) + +(define (parse-app q) ; <----------------------------------------- !!! + (define (parse-app-accum q acc) + (cond [(= 1 (length q)) (app acc (parse (car q)))] + [else (parse-app-accum (cdr q) (app acc (parse (car q))))])) + (parse-app-accum (cdr q) (parse (car q)))) + +(define (parse-lam pat e) ; <------------------------------------- !!! + (cond [(= 1 (length pat)) + (lam (car pat) (parse e))] + [else + (lam (car pat) (parse-lam (cdr pat) e))])) + +; ---------- ; +; Srodowiska ; +; ---------- ; + +(struct environ (xs) #:transparent) + +(define env-empty (environ null)) +(define (env-add x v env) + (environ (cons (cons x v) (environ-xs env)))) +(define (env-lookup x env) + (define (assoc-lookup xs) + (cond [(null? xs) (error "Unknown identifier" x)] + [(eq? x (car (car xs))) (cdr (car xs))] + [else (assoc-lookup (cdr xs))])) + (assoc-lookup (environ-xs env))) + +; --------- ; +; Ewaluacja ; +; --------- ; + +(struct clo (id e env) #:transparent) ; <------------------------- !!! + +(define (value? v) + (or (number? v) + (boolean? v) + (and (pair? v) (value? (car v)) (value? (cdr v))) + (null? v) + (clo? v))) ; <---------------------------------------------- !!! + +(define (op->proc op) + (match op ['+ +] ['- -] ['* *] ['/ /] ['% modulo] + ['= =] ['> >] ['>= >=] ['< <] ['<= <=] + ['and (lambda (x y) (and x y))] + ['or (lambda (x y) (or x y))] + ['not not] ['car car] ['cdr cdr] + [_ false])) ; <--------------------------------------- !!! + +(define (eval-env e env) + (match e + [(const n) n] + [(binop op l r) ((op->proc op) (eval-env l env) + (eval-env r env))] + [(unop op e) ((op->proc op) (eval-env e env))] + [(let-expr x e1 e2) + (eval-env e2 (env-add x (eval-env e1 env) env))] + [(var-expr x) (env-lookup x env)] + [(if-expr eb et ef) (if (eval-env eb env) + (eval-env et env) + (eval-env ef env))] + [(cons-expr e1 e2) (cons (eval-env e1 env) + (eval-env e2 env))] + [(null-expr) null] + [(null?-expr e) (null? (eval-env e env))] + [(lam x e) (clo x e env)] ; <--------------------------------- !!! + [(app f e) ; <------------------------------------------------ !!! + (let ([vf (eval-env f env)] + [ve (eval-env e env)]) + (match vf [(clo x body fun-env) + (eval-env body (env-add x ve fun-env))]))])) + +(define (eval e) (eval-env e env-empty)) + +(define program + '(let [twice (lambda (f x) (f (f x)))] + (let [inc (lambda (x) (+ 1 x))] + (twice twice twice twice inc 1)))) + + +(define (test-eval) (eval (parse program))) \ No newline at end of file diff --git a/semestr-2/racket/lista6/lista8/zad6.bak b/semestr-2/racket/lista6/lista8/zad6.bak new file mode 100644 index 0000000..721f5bf --- /dev/null +++ b/semestr-2/racket/lista6/lista8/zad6.bak @@ -0,0 +1,151 @@ +#lang racket + +; Do list.rkt dodajemy procedury +; +; Miejsca, ktore sie zmienily oznaczone sa przez !!! + +; --------- ; +; Wyrazenia ; +; --------- ; + +(struct const (val) #:transparent) +(struct binop (op l r) #:transparent) +(struct unop (op e) #:transparent) +(struct var-expr (id) #:transparent) +(struct let-expr (id e1 e2) #:transparent) +(struct if-expr (eb et ef) #:transparent) +(struct cons-expr (e1 e2) #:transparent) +(struct null-expr () #:transparent) +(struct null?-expr (e) #:transparent) +(struct app (f e) #:transparent) ; <------------------ !!! +(struct lam (id e) #:transparent) ; <------------------ !!! + + +(define (expr? e) + (match e + [(const n) (or (number? n) (boolean? n))] + [(binop op l r) (and (symbol? op) (expr? l) (expr? r))] + [(var-expr x) (symbol? x)] + [(let-expr x e1 e2) + (and (symbol? x) (expr? e1) (expr? e2))] + [(if-expr eb et ef) + (and (expr? eb) (expr? et) (expr? ef))] + [(cons-expr e1 e2) (and (expr? e1) (expr? e2))] + [(null-expr) true] + [(null?-expr e) (expr? e)] + [(app f e) (and (expr? f) (expr? e))] ; <--------------------- !!! + [(lam id e) (and (symbol? id) (expr? e))] ; <----------------- !!! + [_ false])) + +(define (parse q) + (cond + [(number? q) (const q)] + [(eq? q 'true) (const true)] + [(eq? q 'false) (const false)] + [(eq? q 'null) (null-expr)] + [(symbol? q) (var-expr q)] + [(and (list? q) (eq? (length q) 2) (eq? (first q) 'null?)) + (null?-expr (parse (second q)))] + [(and (list? q) (eq? (length q) 3) (eq? (first q) 'cons)) + (cons-expr (parse (second q)) + (parse (third q)))] + [(and (list? q) (eq? (length q) 3) (eq? (first q) 'let)) + (let-expr (first (second q)) + (parse (second (second q))) + (parse (third q)))] + [(and (list? q) (eq? (length q) 4) (eq? (first q) 'if)) + (if-expr (parse (second q)) + (parse (third q)) + (parse (fourth q)))] + [(and (list? q) (eq? (length q) 3) (eq? (first q) 'lambda)) ; proc (car q)))) ; <------- !!! + (parse-app q)] + [(and (list? q) (eq? (length q) 3) (symbol? (first q))) + (binop (first q) + (parse (second q)) + (parse (third q)))] + [(and (list? q) (eq? (length q) 2) (symbol? (first q))) + (unop (first q) (parse (second q)))])) + +(define (parse-app q) ; <----------------------------------------- !!! + (define (parse-app-accum q acc) + (cond [(= 1 (length q)) (app acc (parse (car q)))] + [else (parse-app-accum (cdr q) (app acc (parse (car q))))])) + (parse-app-accum (cdr q) (parse (car q)))) + +(define (parse-lam pat e) ; <------------------------------------- !!! + (cond [(= 1 (length pat)) + (lam (car pat) (parse e))] + [else + (lam (car pat) (parse-lam (cdr pat) e))])) + +; ---------- ; +; Srodowiska ; +; ---------- ; + +(struct environ (xs) #:transparent) + +(define env-empty (environ null)) +(define (env-add x v env) + (environ (cons (cons x v) (environ-xs env)))) +(define (env-lookup x env) + (define (assoc-lookup xs) + (cond [(null? xs) (error "Unknown identifier" x)] + [(eq? x (car (car xs))) (cdr (car xs))] + [else (assoc-lookup (cdr xs))])) + (assoc-lookup (environ-xs env))) + +; --------- ; +; Ewaluacja ; +; --------- ; + +(struct clo (id e env) #:transparent) ; <------------------------- !!! + +(define (value? v) + (or (number? v) + (boolean? v) + (and (pair? v) (value? (car v)) (value? (cdr v))) + (null? v) + (clo? v))) ; <---------------------------------------------- !!! + +(define (op->proc op) + (match op ['+ +] ['- -] ['* *] ['/ /] ['% modulo] + ['= =] ['> >] ['>= >=] ['< <] ['<= <=] + ['and (lambda (x y) (and x y))] + ['or (lambda (x y) (or x y))] + ['not not] ['car car] ['cdr cdr] + [_ false])) ; <--------------------------------------- !!! + +(define (eval-env e env) + (match e + [(const n) n] + [(binop op l r) ((op->proc op) (eval-env l env) + (eval-env r env))] + [(unop op e) ((op->proc op) (eval-env e env))] + [(let-expr x e1 e2) + (eval-env e2 (env-add x (eval-env e1 env) env))] + [(var-expr x) (env-lookup x env)] + [(if-expr eb et ef) (if (eval-env eb env) + (eval-env et env) + (eval-env ef env))] + [(cons-expr e1 e2) (cons (eval-env e1 env) + (eval-env e2 env))] + [(null-expr) null] + [(null?-expr e) (null? (eval-env e env))] + [(lam x e) (clo x e env)] ; <--------------------------------- !!! + [(app f e) ; <------------------------------------------------ !!! + (let ([vf (eval-env f env)] + [ve (eval-env e env)]) + (match vf [(clo x body fun-env) + (eval-env body (env-add x ve fun-env))]))])) + +(define (eval e) (eval-env e env-empty)) + +(define program + '(let [twice (lambda (f x) (f (f x)))] + (let [inc (lambda (x) (+ 1 x))] + (twice twice twice twice inc 1)))) + + +(define (test-eval) (eval (parse program))) \ No newline at end of file diff --git a/semestr-2/racket/lista6/lista8/zad6.rkt b/semestr-2/racket/lista6/lista8/zad6.rkt new file mode 100644 index 0000000..c7ea9f0 --- /dev/null +++ b/semestr-2/racket/lista6/lista8/zad6.rkt @@ -0,0 +1,171 @@ +#lang racket + +; Do list.rkt dodajemy procedury +; +; Miejsca, ktore sie zmienily oznaczone sa przez !!! + +; --------- ; +; Wyrazenia ; +; --------- ; + +(struct const (val) #:transparent) +(struct binop (op l r) #:transparent) +(struct unop (op e) #:transparent) +(struct var-expr (id) #:transparent) +(struct let-expr (id e1 e2) #:transparent) +(struct if-expr (eb et ef) #:transparent) +(struct cons-expr (e1 e2) #:transparent) +(struct null-expr () #:transparent) +(struct null?-expr (e) #:transparent) +(struct app (f e) #:transparent) ; <------------------ !!! +(struct lam (id e) #:transparent) ; <------------------ !!! + +(define (expr? e) + (match e + [(const n) (or (number? n) (boolean? n))] + [(unop op e) (and (symbol? op) (expr? e))] + [(binop op l r) (and (symbol? op) (expr? l) (expr? r))] + [(var-expr x) (symbol? x)] + [(let-expr x e1 e2) + (and (symbol? x) (expr? e1) (expr? e2))] + [(if-expr eb et ef) + (and (expr? eb) (expr? et) (expr? ef))] + [(cons-expr e1 e2) (and (expr? e1) (expr? e2))] + [(null-expr) true] + [(null?-expr e) (expr? e)] + [(app f e) (and (expr? f) (expr? e))] ; <--------------------- !!! + [(lam id e) (and (symbol? id) (expr? e))] ; <----------------- !!! + [_ false])) + +(define (cedar? f) + (let ((letters (string->list (symbol->string f)))) + (and (> (length letters) 2) + (eq? (first letters) #\c) + (eq? (first (reverse letters)) #\r) + (andmap (lambda (x) (or (eq? x #\a) (eq? x #\d))) + (cdr letters))))) + +(define (get-cedar letters xs) + (cond [(eq? (car letters) #\r) xs] + [(eq? (car letters) #\a) (unop 'car (get-cedar (cdr letters) xs))] + [(eq? (car letters) #\d) (unop 'cdr (get-cedar (cdr letters) xs))])) + +(define (cedar f xs) + (let ((letters (string->list (symbol->string f)))) + (get-cedar (cdr (reverse letters)) xs))) + +(define (parse q) + (cond + [(number? q) (const q)] + [(eq? q 'true) (const true)] + [(eq? q 'false) (const false)] + [(eq? q 'null) (null-expr)] + [(symbol? q) (var-expr q)] + [(and (list? q) (eq? (length q) 2) (eq? (first q) 'null?)) + (null?-expr (parse (second q)))] + [(and (list? q) (eq? (length q) 3) (eq? (first q) 'cons)) + (cons-expr (parse (second q)) + (parse (third q)))] + [(and (list? q) (eq? (length q) 3) (eq? (first q) 'let)) + (let-expr (first (second q)) + (parse (second (second q))) + (parse (third q)))] + [(and (list? q) (eq? (length q) 4) (eq? (first q) 'if)) + (if-expr (parse (second q)) + (parse (third q)) + (parse (fourth q)))] + [(and (list? q) (eq? (length q) 3) (eq? (first q) 'lambda)) ; proc (car q)))) ; <------- !!! + (parse-app q)] + [(and (list? q) (eq? (length q) 3) (symbol? (first q))) + (binop (first q) + (parse (second q)) + (parse (third q)))] + [(and (list? q) (eq? (length q) 2) (symbol? (first q))) + (unop (first q) (parse (second q)))])) + +(define (parse-app q) ; <----------------------------------------- !!! + (define (parse-app-accum q acc) + (cond [(= 1 (length q)) (app acc (parse (car q)))] + [else (parse-app-accum (cdr q) (app acc (parse (car q))))])) + (parse-app-accum (cdr q) (parse (car q)))) + +(define (parse-lam pat e) ; <------------------------------------- !!! + (cond [(= 1 (length pat)) + (lam (car pat) (parse e))] + [else + (lam (car pat) (parse-lam (cdr pat) e))])) + +; ---------- ; +; Srodowiska ; +; ---------- ; + +(struct environ (xs) #:transparent) + +(define env-empty (environ null)) +(define (env-add x v env) + (environ (cons (cons x v) (environ-xs env)))) +(define (env-lookup x env) + (define (assoc-lookup xs) + (cond [(null? xs) (error "Unknown identifier" x)] + [(eq? x (car (car xs))) (cdr (car xs))] + [else (assoc-lookup (cdr xs))])) + (assoc-lookup (environ-xs env))) + +; --------- ; +; Ewaluacja ; +; --------- ; + +(struct clo (id e env) #:transparent) ; <------------------------- !!! + +(define (value? v) + (or (number? v) + (boolean? v) + (and (pair? v) (value? (car v)) (value? (cdr v))) + (null? v) + (clo? v))) ; <---------------------------------------------- !!! + +(define (op->proc op) + (match op ['+ +] ['- -] ['* *] ['/ /] ['% modulo] + ['= =] ['> >] ['>= >=] ['< <] ['<= <=] + ['and (lambda (x y) (and x y))] + ['or (lambda (x y) (or x y))] + ['not not] ['car car] ['cdr cdr] + [_ false])) ; <--------------------------------------- !!! + +(define (eval-env e env) + (match e + [(const n) n] + [(binop op l r) ((op->proc op) (eval-env l env) + (eval-env r env))] + [(unop op e) ((op->proc op) (eval-env e env))] + [(let-expr x e1 e2) + (eval-env e2 (env-add x (eval-env e1 env) env))] + [(var-expr x) (env-lookup x env)] + [(if-expr eb et ef) (if (eval-env eb env) + (eval-env et env) + (eval-env ef env))] + [(cons-expr e1 e2) (cons (eval-env e1 env) + (eval-env e2 env))] + [(null-expr) null] + [(null?-expr e) (null? (eval-env e env))] + [(lam x e) (clo x e env)] ; <--------------------------------- !!! + [(app f e) ; <------------------------------------------------ !!! + (let ([vf (eval-env f env)] + [ve (eval-env e env)]) + (match vf [(clo x body fun-env) + (eval-env body (env-add x ve fun-env))]))])) + +(define (eval e) (eval-env e env-empty)) + +(define program + '(let [twice (lambda (f x) (f (f x)))] + (let [inc (lambda (x) (+ 1 x))] + (twice twice twice twice inc 1)))) + + +(define (test-eval) (eval (parse program))) \ No newline at end of file diff --git a/semestr-2/racket/lista6/lista8/zadanie.rkt b/semestr-2/racket/lista6/lista8/zadanie.rkt new file mode 100644 index 0000000..0960f21 --- /dev/null +++ b/semestr-2/racket/lista6/lista8/zadanie.rkt @@ -0,0 +1,98 @@ +#lang racket + +; Do let-env.rkt dodajemy wartosci boolowskie +; +; Miejsca, ktore sie zmienily oznaczone sa przez !!! + +; --------- ; +; Wyrazenia ; +; --------- ; + +(struct const (val) #:transparent) +(struct binop (op l r) #:transparent) +(struct var-expr (id) #:transparent) +(struct let-expr (id e1 e2) #:transparent) +(struct if-expr (eb et ef) #:transparent) + +(define (expr? e) + (match e + [(const n) (or (number? n) (boolean? n))] ; <----------------- !!! + [(binop op l r) (and (symbol? op) (expr? l) (expr? r))] + [(var-expr x) (symbol? x)] + [(let-expr x e1 e2) + (and (symbol? x) (expr? e1) (expr? e2))] + [(if-expr eb et ef) ; <--------------------------------------- !!! + (and (expr? eb) (expr? et) (expr? ef))] + [_ false])) + +(define (parse q) + (cond + [(number? q) (const q)] + [(eq? q 'true) (const true)] ; <---------------------------- !!! + [(eq? q 'false) (const false)] ; <---------------------------- !!! + [(symbol? q) (var-expr q)] + [(and (list? q) (eq? (length q) 3) (eq? (first q) 'let)) + (let-expr (first (second q)) + (parse (second (second q))) + (parse (third q)))] + [(and (list? q) (eq? (length q) 4) (eq? (first q) 'if)) ; <--- !!! + (if-expr (parse (second q)) + (parse (third q)) + (parse (fourth q)))] + [(and (list? q) (eq? (length q) 3) (symbol? (first q))) + (binop (first q) + (parse (second q)) + (parse (third q)))])) + +(define (test-parse) (parse '(let [x (+ 2 2)] (+ x 1)))) + +; ---------- ; +; Srodowiska ; +; ---------- ; + +(struct environ (xs)) + +(define env-empty (environ null)) +(define (env-add x v env) + (environ (cons (cons x v) (environ-xs env)))) +(define (env-lookup x env) + (define (assoc-lookup xs) + (cond [(null? xs) (error "Unknown identifier" x)] + [(eq? x (car (car xs))) (cdr (car xs))] + [else (assoc-lookup (cdr xs))])) + (assoc-lookup (environ-xs env))) + +; --------- ; +; Ewaluacja ; +; --------- ; + +(define (value? v) + (or (number? v) (boolean? v))) + +(define (op->proc op) + (match op ['+ +] ['- -] ['* *] ['/ /] ['% modulo] ; <----------- !!! + ['= =] ['> >] ['>= >=] ['< <] ['<= <=] + ['and (lambda (x y) (and x y))] + ['or (lambda (x y) (or x y))])) + +(define (eval-env e env) + (match e + [(const n) n] + [(binop op l r) ((op->proc op) (eval-env l env) + (eval-env r env))] + [(let-expr x e1 e2) + (eval-env e2 (env-add x (eval-env e1 env) env))] + [(var-expr x) (env-lookup x env)] + [(if-expr eb et ef) (if (eval-env eb env) ; <----------------- !!! + (eval-env et env) + (eval-env ef env))])) + +(define (eval e) (eval-env e env-empty)) + +(define program + '(if (or (< (% 123 10) 5) + true) + (+ 2 3) + (/ 2 0))) + +(define (test-eval) (eval (parse program))) \ No newline at end of file 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 diff --git a/semestr-2/racket/lista6/solution.rkt b/semestr-2/racket/lista6/solution.rkt new file mode 100644 index 0000000..59bdecd --- /dev/null +++ b/semestr-2/racket/lista6/solution.rkt @@ -0,0 +1,73 @@ +#lang racket + +(provide (struct-out complex) parse eval) + +(struct complex (re im) #:transparent) + +(define value? + complex?) + +(define (comp-plus x y) + (let ((x-re (complex-re x)) + (x-im (complex-im x)) + (y-re (complex-re y)) + (y-im (complex-im y))) + (complex (+ x-re y-re) (+ x-im y-im)))) + +(define (comp-minus x y) + (let ((x-re (complex-re x)) + (x-im (complex-im x)) + (y-re (complex-re y)) + (y-im (complex-im y))) + (complex (- x-re y-re) (- x-im y-im)))) + +(define (comp-mult x y) + (let ((x-re (complex-re x)) + (x-im (complex-im x)) + (y-re (complex-re y)) + (y-im (complex-im y))) + (complex (- (* x-re y-re) (* x-im y-im)) (+ (* x-re y-im) (* x-im y-re))))) + +(define (comp-mod2 x) + (let ((x-re (complex-re x)) + (x-im (complex-im x))) + (complex (+ (* x-re x-re) (* x-im x-im)) 0))) + +(define (comp-mod x) + (let ((mod2 (comp-mod2 x)) + (x-re (complex-re x))) + (complex (sqrt x-re) 0))) + +(define (comp-div x y) + (let* ((mod2 (complex-re (comp-mod2 y))) + (x-re (complex-re x)) + (x-im (complex-im x)) + (y-re (complex-re y)) + (y-im (complex-im y)) + (real (+ (* x-re y-re) (* x-im y-im))) + (imag (- (* x-im y-re) (* x-re y-im)))) + (complex (/ real mod2) (/ imag mod2)))) + + +;; 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 (imaginary-unit? c) + (eq? c 'i)) + +(define (op->proc op) + (match op ['+ comp-plus] ['- comp-minus] ['* comp-mult] ['/ comp-div])) + +(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 (complex q 0))] + [(imaginary-unit? q) (const (complex 0 1))] + [(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 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 -- cgit v1.2.3