Post1up 03-Simpledata
Post1up 03-Simpledata
This is asking “Is it true that the value of x is less than 5?”
If we evaluate (< x 5), we substitute in the value of the constant, so our expression
becomes (< 2 5). Since it is true that 2 < 5, the statement evaluates to true.
On the other hand, if we define the constant:
(define y 10)
Now (< y 5) ⇒ (< 10 5) ⇒ false since it is not the case that 10 < 5.
Booleans (Bool) M03 4/39
trueand false are values, just like 0, 100, and 22/7 are values. true and false are
Boolean values; the others are numeric values.
<, >, <=, >=, and = are functions, each of which produces a value, abbreviated Bool in
contracts.
(define x 4)
(< x 6) ⇒ is x (4) less than 6?
(> x 6) ⇒ is x greater than 6?
(= x 7) ⇒ is x equal to 7?
(>= 5 x) ⇒ is 5 greater than or equal to x?
(<= 5 x) ⇒ is 5 less than or equal to x?
Each produces true or false. These are the only values a Bool may take.
Predicates M03 5/39
A function which produces a Bool is called a predicate. For many predicates in Racket, the
name ends with ?.
We can also write our own predicates. For example:
2 even?
3 =
4 negative?
Combining predicates M03 6/39
Our previous version of can-vote? is too simplistic. In reality, you need to be at least 18
years old and a citizen.
We combine predicates using the special forms and and or, and the function not. These all
consume and produce Bool values.
Combining predicates M03 7/39
We combine predicates using the special forms and and or, and the function not. These all
consume and produce Bool values.
and has value true when all of its arguments have value true; false otherwise.
or produces true if at least one of its arguments is true; false otherwise.
not produces true if its argument is false; false if its argument is true.
Both or and and require at least two arguments, but may have more.
Examples M03 8/39
(check-expect
(weak-password? "fooBar") true)
Write a function that consumes an Int, and produces
"baz" for even numbers in the interval [10, 40]
Ex. 2
Racket only evaluates as many arguments of and and or as is necessary to determine the
value. Examples:
(define s "bravo")
(and (> 7 4) true (string=? s "bravo"))
Rules 7-9: Substitution rules for or M03 12/39
(or) ⇒ false
Ex. 5 Perform a trace of
Perform a trace of
Ex. 6
(define s "bravo")
(or (< 7 4) false (string=? s "hooray"))
Conditional expressions M03 13/39
Sometimes expressions should take one value under some conditions, and other values
under other conditions.
2
A sin-squared window, used in signal
processing, can be described by the following
piecewise function:
1
0 for x < 0
f (x) = 1 for x ≥ 1
sin2 (xπ/2)
for 0 ≤ x < 1
0
−1 0 1 2
−1
> Conditional expressions (cont.) M03 14/39
We can compute the sin-squared window function f (x) with a conditional expression:
(cond [(< x 0) 0]
[(>= x 1) 1]
[(< x 1) (sqr (sin (* x pi 0.5)))])
(define (ssqw x)
(cond The second test has changed
[(< x 0) 0] from >= to just >.
[(> x 1) 1]
[(< x 1) (sqr (sin (* x pi 0.5)))]))
This can be helpful – if we see this error we know we’ve missed a case in our code.
else M03 17/39
But sometimes we want to only describe some conditions, and do something different if
none of them are satisfied.
In these situations, the question in the last question/answer pair may be else.
(define (ssqw x)
(cond
[(< x 0) 0]
[(>= x 1) 1]
[else (sqr (sin (* x pi 0.5)))]))
Rules 10-12: Substitution in cond expressions M03 18/39
There are three rules: when the first expression is false, when it is true, and when it is else.
What happens if y is not defined? DrRacket’s rules differ. It scans the whole
cond expression before it starts, notes that
y is not defined, and shows an error. That’s
hard to explain with substitution rules!
Step through this program
(define (qux a b)
(cond
[(= a b) 42]
[(> a (+ 3 b)) (* a b)]
Ex. 7
[(> a b) (- b a)]
[else -42]))
(qux 5 4)
40 50 60
40 50 60
(define (flatten-me x)
(cond [(>= x 75) 4]
[(and (>= x 50) (< x 75)) 3]
[(and (>= x 25) (< x 50)) 2]
[(< x 25) 1]))
Nested Conditionals M03 22/39
[else (cond ... is considered amateurish code because it can always be easily flattened.
Testing conditional expressions M03 24/39
Write at least one test for each possible answer in the conditional expression.
That test should be simple and direct, aimed at testing that answer.
When the problem contains boundary conditions (like the cut-off between passing
and failing marks), they should be tested explicitly.
DrRacket highlights unused code.
> Intervals for testing M03 25/39
there are four intervals and three boundary points, so seven tests are required (for example,
35, 40, 45 50, 55, 60, 70).
Write a function that consumes a Num, x, and produces
1 if 80 < x ≤ 100,
Ex. 9
-1 if 0 < x ≤ 80,
0 otherwise.
Write tests to verify the boundaries are where they should be.
> Testing and and or M03 26/39
Examine your code for tax-payable. Are there opportunitites to improve it with a helper
Ex. 11
function? (Unless you used one the first time, the answer is probably "yes!". Look for
repeated code.)
Implement tax-payable using a helper function.
Symbolic data M03 28/39
Racket allows one to define and use symbols with meaning to us (not to Racket).
A symbol is defined using a leading apostrophe or ‘quote’: 'CS115. What follows is the same
as any other identifer.
'CS115 is a value just like 0 or 115, but it is more limited computationally.
Symbols allow a programmer to avoid using constants to represent names of courses,
colours, planets, or types of music.
Unlike numbers, symbols are self-documenting – you don’t need to define constants for
them. This is the primary reason we use them.
course-after-CS135 revisited M03 29/39
symbol=? is the only function we’ll use in CS135 that is applied only to symbols.
Like other types, there is a predicate: symbol?.
Strings are sequences of characters between double quotes. Examples: "blue" and
"These are not my shoes. My shoes are brown.".
Non-numeric types also have predicates. For example, these predicates consume strings
and will be useful when we do more work with strings.
We can tell if two strings are the same:
We can also tell if a pair of strings are in alphabetic order. If one string comes before
another, it is “less than” it. If it comes after, it is “greater than”. Some examples:
Consider the use of symbols when a small, fixed number of labels are needed (e.g. planets)
that only need to be compared for equality.
Use strings when the set of values is more indeterminate (e.g. names of students), or when
more computation is needed (e.g. comparison in alphabetical order).
Type Predicates M03 36/39
Each built-in type has a predicate that consumes an Any, and produces true if the value is
of that type, and false otherwise.
For example:
(symbol? 4) ⇒ false
(symbol? 'asdf) ⇒ true
(number? "42") ⇒ false
(number? 42) ⇒ true
(integer? 3.14) ⇒ false
(integer? -3) ⇒ true
(boolean? true) ⇒ true
(boolean? false) ⇒ true
(boolean? "George Boole") ⇒ false
(string? 42) ⇒ false
Recap: Substitution rules (so far) M03 37/39
You should understand Boolean data, and be able to perform and combine
comparisons to test complex conditions on numbers.
You should understand the syntax and use of a conditional expression.
You should be aware of other types of data (symbols and strings), which will be used in
future lectures.
You should understand how to write tests with check-expect and use them in your
assignment submissions.
You should look for opportunities to use helper functions to structure your programs,
and gradually learn when and where they are appropriate.
You should be able to trace a program using the twelve substitution rules we’ve defined
so far.
Summary: built-in functions M03 39/39
The following functions and special forms have been introduced in this module:
< <= = > >= and boolean? check-error cond else even? integer? negative? not
number->string number? odd? or positive? string-append string-downcase
string-length string-lower-case? string-numeric? string-upcase
string-upper-case? string<=? string<? string=? string>=? string>? string?
substring symbol=? symbol? zero?
You should complete all exercises and assignments using only these and the functions and
special forms introduced in earlier modules. The complete list is:
* + - / < <= = > >= abs and boolean? ceiling check-error check-expect check-within cond
cos define e else even? exp expt floor integer? log max min modulo negative? not
number->string number? odd? or pi positive? quotient remainder round sgn sin sqr sqrt
string-append string-downcase string-length string-lower-case? string-numeric?
string-upcase string-upper-case? string<=? string<? string=? string>=? string>?
string? substring symbol=? symbol? tan zero?