; CS 100 - Week 2 Lecture 2 - 8-30-12 ; playing with Boolean expressions using and, or, and not, ; in DrRacket's Beginning Student language ; anything after a semicolon up to the end of the line is a COMMENT ; in Racket, ignored by the computer, used to give info to ; PEOPLE reading a program ; Boolean and ; (and expr1 expr2 ...) ; ^ this and operator will result in true ; if ALL of the Boolean expressions following ; it are true ; an example of a truth table (rather-informal style) ; (this one is for the Boolean and operation) ; expr1 expr2 (and expr1 expr2) ; true true true ; true false false ; false true false ; false false false ; you can give a name a value in Racket ; with the define operation ; (define a_name expr) ; Boolean or ; (or expr1 expr2 ...) ; ^ operator or is true if at least one of its ; expressions is true ; informal truth table for the Boolean or operation ; expr1 expr2 (or expr1 expr2) ; true true true ; true false true ; false true true ; false false false ; Boolean not ; (not expr) ; ^ the not operator expects a single Boolean ; expression, and produces true if that ; expression is false, and false if it is true ; informal truth table for the Boolean not operation ; expr (not expr) ; true false ; false true ; you can make truth tables for any Boolean expression ; using Boolean operators... ;(or (and expr1 expr2) ; (and expr2 expr3)) ; e1 e2 e3 (and e1 e2)(and e2 e3)(or (and e1 e2) ; (and e2 e3)) ;----------------------------------------------- ; t t t t t t ; t t f t f t ; t f t f f f ; t f f f f f ; f t t f t t ; f t f f f f ; f f t f f f ; f f f f f f ; using define to define variables and give them values; ; and then showing you can use Racket to get the values ; of compound Boolean expressions using those values (define is_raining true) (define is_cold false) (or is_cold (not is_raining)) ; a predicate function returns true or false; ; DrRacket's Beginning Student language ; has a bunch of type predicates: ; string?: any expression -> IF it is a string ; boolean?: any expr -> IF it is a boolean ; number?: any expr -> IF it is a number ; PASTED in Interactions window, in a comment, to show some things we ; tried in-class ;Welcome to DrRacket, version 5.1.3 [3m]. ;Language: Beginning Student [custom]; memory limit: 256 MB. ;false ;> (or is_cold (not is_raining)) ;false ;> (number? "3") ;false ;> (string? "3") ;true ;> (boolean? "3") ;false ;> (boolean? true) ;true ;> (boolean? (and true false)) ;true ;> (number? (+ 3 5)) ;true ;> (string-length "fifth") ;5 ;> (number? (string-length "moo")) ;true ;> (= 5 (+ 4 1)) ;true ;> (equal? "strong" "strong") ;true ;> (equal? "strong" "weak") ;false ;> (= 3 (+ 4 1)) ;false ;> (equal? "strong" strong) ;strong: this variable is not defined ;> ;