131 lines
4.2 KiB
Plaintext
131 lines
4.2 KiB
Plaintext
;; The first three lines of this file were inserted by DrScheme. They record metadata
|
|
;; about the language level of this file in a form that our tools can easily process.
|
|
#reader(lib "htdp-beginner-reader.ss" "lang")((modname trees) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ())))
|
|
;;(c) Ulf Gebhardt
|
|
|
|
;;API-Def
|
|
;;
|
|
;;
|
|
|
|
;; a bool-direct represents direct boolean values
|
|
;; value: direct-boolean - a boolean value that can be either
|
|
;; 1. a boolean value, i.e., something that evaluates to true or false,
|
|
;; 2. or a symbol that represents one of the variables 'A...'F
|
|
(define-struct bool-direct (value))
|
|
|
|
;; bool-unary represents unary boolean operators
|
|
;; op: symbol - a legal unary operator (e.g., 'not)
|
|
;; param: bool-tree - a boolean expression
|
|
(define-struct bool-unary (op param))
|
|
|
|
;; bool-binary represents binary boolean operators
|
|
;; op: symbol - a legal binary operator (e.g., 'and, 'or)
|
|
;; left: bool-tree - the left (2.) part of the binary boolean expression
|
|
;; right: bool-tree - the right (3.) part of the binary boolean expr.
|
|
(define-struct bool-binary (op left right))
|
|
|
|
;; lookup-variable: symbol -> boolean
|
|
;; looks up the value of the symbol passed in
|
|
;; if undefined, returns an error
|
|
;; example: (lookup-variable 'A) is true
|
|
(define (lookup-variable variable)
|
|
(cond
|
|
[(symbol=? variable 'A) true]
|
|
[(symbol=? variable 'B) false]
|
|
[(symbol=? variable 'C) true]
|
|
[(symbol=? variable 'D) false]
|
|
[(symbol=? variable 'E) false]
|
|
[(symbol=? variable 'F) true]
|
|
[else (error 'lookup-variable
|
|
(string-append "Variable "
|
|
(symbol->string variable)
|
|
" unknown"))]
|
|
))
|
|
|
|
;; Tests
|
|
(check-expect (lookup-variable 'A) true)
|
|
(check-expect (lookup-variable 'B) false)
|
|
(check-expect (lookup-variable 'C) true)
|
|
(check-expect (lookup-variable 'D) false)
|
|
(check-expect (lookup-variable 'E) false)
|
|
(check-expect (lookup-variable 'F) true)
|
|
(check-error (lookup-variable 'G) "lookup-variable: Variable G unknown")
|
|
|
|
|
|
;;-------------------------------------------------------------------------------------------------------
|
|
;;-------------------------------------------CODE--------------------------------------------------------
|
|
;;-------------------------------------------------------------------------------------------------------
|
|
;; a bool-input is either
|
|
;; 1. a boolean value (true,false,an expression evaluated to true/false)
|
|
;; 2. a symbol 'A...'F for a boolean variable
|
|
;; 3. (list 'not b), where b is a bool-input
|
|
;; 4. (list 'and b1 b2), where b1 and b2 have type bool-input
|
|
;; 5. (list 'or b1 b2), where b1 and b2 have type bool-input
|
|
|
|
(define (bool-input value)
|
|
(if (or (bool? value)
|
|
(symbol? value)
|
|
(cons? value))
|
|
value ;;return value
|
|
error("Get lost, this suxxs") ;;Change that shit
|
|
)
|
|
)
|
|
|
|
;; a bool-tree is either
|
|
;; 1. a bool-direct (boolean value or boolean variable)
|
|
;; 2. a bool-unary (unary operator, i.e., 'not)
|
|
;; 3. a bool-binary (binary operator, e.g., 'and, 'or)
|
|
(define-struct bool_tree (r a b))
|
|
|
|
(define (bool-tree r a b)
|
|
|
|
)
|
|
;;
|
|
;;
|
|
;;
|
|
;;(define (input->tree input)
|
|
;; (cond [(bool? input)]
|
|
;; []
|
|
;;)
|
|
|
|
;; Tests
|
|
(check-expect (input->tree (list 'and 'A true))
|
|
(make-bool-binary 'and
|
|
(make-bool-direct 'A)
|
|
(make-bool-direct true)))
|
|
(check-expect (input->tree (list 'or (list 'not 'A) 'B))
|
|
(make-bool-binary 'or
|
|
(make-bool-unary 'not
|
|
(make-bool-direct 'A))
|
|
(make-bool-direct 'B)))
|
|
|
|
|
|
;;
|
|
;;
|
|
;;
|
|
;;
|
|
(define (eval-unary unary-op unary-param) ...)
|
|
|
|
;; Tests
|
|
(check-expect (eval-unary 'not true) false)
|
|
|
|
;;
|
|
;;
|
|
;;
|
|
;;
|
|
(define (eval-binary op left right) ...)
|
|
|
|
;; Tests
|
|
(check-expect (eval-binary 'and true true) true)
|
|
(check-expect (eval-binary 'or false true) true)
|
|
|
|
;;
|
|
;;
|
|
;;
|
|
(define (bool-tree->boolean a-bool-tree) ...)
|
|
|
|
;; Tests
|
|
(check-expect (bool-tree->boolean (input->tree (list 'and 'A true)))
|
|
true)
|
|
(check-expect (bool-tree->boolean (input->tree (list 'or (list 'not 'A) 'B)))
|
|
false) |