Lecture 6 Slides - Lecture Slides

1 of 25

Lecture 6

Boolean Expressions and Conditionals

2 of 25

Boolean expressions

3 of 25

Boolean objects - A new data type

  • Racket code is made up of expressions
    • (= a b) is an expression
  • And all expressions produce outputs (data objects) when they’re executed
    • So then what kind of data object does (= a b) produce?
  • Answer: a truth value true or false
    • These are named Booleans after George Boole, who invented Boolean Algebra, an early form of symbolic logic

4 of 25

Boolean constants

  • Racket has two magic data objects that are used to represent the answers to questions
    • They’re named true and false.
  • For historical reasons, you can type them a few different ways:
    • True: true, #true, #t
    • False: false, #false, #f
  • You can use any of these
    • We’ll generally use true and false in slides
    • But DrRacket always prints them in # format

5 of 25

A new kind of expression: if

(if test consequent alternative)

  • If test is true,
    • Then execute and return consequent
    • Otherwise, execute and return alternative
  • Some useful built-in tests
    • (string=? a b)�Checks if a and b are the same string
    • (= a b)

Checks if a and b are the same number

    • (> a b), (<= a b), etc.

Compares numbers

;; absolute-value: num -> num

;; Removes the sign from a num

> (define absolute-value

(λ (n)

(if (> n 0)

n

(- n))))

> (absolute-value 5)

5

> (absolute-value -5)

5

Note: absolute value is already in Racket and is called abs. We just use it here because it’s a simple example.

6 of 25

new rules of computation

7 of 25

Some other built-in tests

  • (number? value), (string? value),�(integer? value), (procedure? value)

Tests what kind of data value is

  • (odd? number), (even? number)

Tests whether a number is odd or even

  • (and test1 test2testn)�(or test1 test2testn)�(not test)

Combines tests into more complicated tests

Note: Racket sometimes calls "functions" a different word: procedures. Functionally, they're the same in our class, but this function here, which tests to see if the given value is a function is actually named procedure? Rather than function?

8 of 25

Examples

Suppose we say:

(define a 7)

(define b 8)

(define c 9.5)

What are the values of:

> (> a b)

>

> (< a b)

>

> (not (= b c))

>

> (integer? c)

>

> (odd? a)

>

> (and (< 5 a)

(< a 10))

>

9 of 25

Examples

Suppose we say:

(define a 7)

(define b 8)

(define c 9.5)

What are the values of:

> (> a b)

> #false

> (< a b)

> #true

> (not (= b c))

> #true

> (integer? c)

> #false

> (odd? a)

> #true

> (and (< 5 a)

(< a 10))

> #true

10 of 25

Executing a Boolean expression

Evaluating tests is really just normal function* execution

(and (< 5 a)� (< a 10))

  • First, execute (< 5 a)
    • Call < with 5 and 7 as inputs
    • < returns true
  • Then call (< a 10)
    • Call < with 7 and 10 as inputs
    • < returns true
  • Call and with true and true as arguments
    • (this part is a little more complicated, but that won’t matter until CS 211)
  • And returns true

*Truth in advertising: and isn’t actually a function, it’s a special form. So this slide is a lie. But it’s a useful lie.

11 of 25

Predicates (question answer-ers)

  • Functions that return Booleans, e.g. = or odd?, are called predicates
    • They can be thought of as tests or question answer-ers
      • (= 1 2) asks the question “are 1 and 2 the same?”
      • (odd? 7) asks the question “is 7 an odd number?”

    • And their return values can be thought of as the answers
      • (= 1 2) returns false
      • (odd? 7) returns true

  • Predicates are an important type of function

12 of 25

User-defined predicates

  • Predicates are just normal functions that happen to return Booleans
  • So you can write your own just like any other kind of function

;; perfect-square?: number -> Boolean

;; True if arg is the square of an

;; integer

> (define perfect-square?

(λ (n)

(integer? (sqrt n))))

> (perfect-square? 4)

#true

> (perfect-square? 3)

#false

13 of 25

Testing equality

As with other languages, Racket has a few different notions of two objects being the same

  • They can be literally the same object in memory
  • They can be different, but equivalent objects

So it has a few different functions for testing equality

(eq? a b)�The two arguments are literally the same object in memory

    • 4 is not eq? to 4.0
    • Strings may not be eq? to one another

(eqv? a b)�Arguments are the same object or equal numbers.

(equal? a b)�The two arguments are equivalent objects. Works for numbers, strings, and some compound data.

14 of 25

Testing equality

As with other languages, Racket has a few different notions of two objects being the same

  • They can be literally the same object in memory
  • They can be different, but equivalent objects

So it has a few different functions for testing equality. For the moment we only care about these two:

(= number1 number2)True when the numbers are the same and throws an exception if they aren’t numbers.

(string=? string1 string2)True when the strings are the same and throws an exception if they aren’t strings

15 of 25

If demos

16 of 25

iferation!

17 of 25

Got a lot of cases?

18 of 25

That's what cond is for!

(if C1 R1 R2) ; if C1 is true, then R1 otherwise R2

(if (> x 5) "big" "small")

(cond [C1 R1] ; if C1 is true, then R1

[C2 R2] ; otherwise if C2 is true, then R2

[Cn Rn]); otherwise if Cn is true, then Rn

(cond [(> x 5) "big"] ; if x > 5, then "big"

[(= x 5) "medium"] ; otherwise if x = 5, "medium"

[else "small"]) ; otherwise "small"

19 of 25

Local variables

Another abstraction tool

20 of 25

Local names in Racket

(local [(define name1 value1)

(define name2 value2)

(define namen valuen)]�output-expression)

  • Local names (also known as local variables or temporary variables) are names that are only in effect for a single expression
  • Sort of like pronouns in English

21 of 25

Local names in Racket

(local [(define name1 value1)

(define name2 value2)

(define namen valuen)]�output-expression)

  • Computes all the values
  • Substitutes them for their respective names in output-expression
  • Runs output-expression and returns its value

22 of 25

Feeling lost?

23 of 25

What data is encoded here?

24 of 25

What data is encoded here?

25 of 25

What data is encoded here?