Lecture 6
Boolean Expressions and Conditionals
Boolean expressions
Boolean objects - A new data type
Boolean constants
A new kind of expression: if
(if test consequent alternative)
Checks if a and b are the same number
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.
new rules of computation
Some other built-in tests
Tests what kind of data value is
Tests whether a number is odd or even
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?
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))
>
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
Executing a Boolean expression
Evaluating tests is really just normal function* execution
(and (< 5 a)� (< a 10))
*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.
Predicates (question answer-ers)
User-defined predicates
;; 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
Testing equality
As with other languages, Racket has a few different notions of two objects being the same
So it has a few different functions for testing equality
(eq? a b)�The two arguments are literally the same object in memory
(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.
Testing equality
As with other languages, Racket has a few different notions of two objects being the same
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
If demos
iferation!
Got a lot of cases?
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"
Local variables
Another abstraction tool
Local names in Racket
(local [(define name1 value1)
(define name2 value2)
…
(define namen valuen)]�output-expression)
Local names in Racket
(local [(define name1 value1)
(define name2 value2)
…
(define namen valuen)]�output-expression)
Feeling lost?
What data is encoded here?
What data is encoded here?
What data is encoded here?