=====
CS 111 - Week 4 Lecture 1 - 2026-09-15
=====
=====
TODAY WE WILL:
=====
* announcements
* Boolean operations and or not
* concept of REFACTORING
* intro to branching: cond expression
* big-bang's stop-when clause
* maybe more...
* prep for next class
=====
* should be working on Homework 3
deadline/at least first attempts due by this Friday, Sept. 18
* submit early, submit often!
* Readings for Week 4 are now posted on Canvas and the
public course web site
=====
Boolean operations: and or not
=====
* these all expect boolean argument(s) and
return a boolean value
* signature: and: boolean boolean ... -> boolean
purpose: expects 2 or more boolean arguments,
and returns whether ALL of the arguments are true
* signature: or: boolean boolean ... -> boolean
purpose: expects 2 or more boolean arguments,
and returns whether AT LEAST ONE of the arguments
are true
* signature: not: boolean -> boolean
purpose: expects ONE boolean argument,
and returns #true if the argument is #false,
and returns #false if the argument is #true
* see some examples in today's posted Racket file
=====
refactoring
=====
* debugging: fixing an error in a program
* sometimes you extend or add to or make modifications
to a program based on client preferences, etc.
* refactoring is not either of those --
it means taking WORKING code and improving it
without actually changing what it does;
* why?!
* maybe:
* to make it more readable
* to make it more efficient
* to make it easier to reuse
* etc.!
* NOTE: ANY time you change your code, there is a chance you
might accidentally introduce an error --
do you see how check-expect (and other check- expressions)
may help make refactoring less dangerous, since these are
re-run every time you click Run?
so if all your tests still pass after you refactor, that
increases your confidence that you have not introduced
an error while refactoring; (not a guarateee, but helps!)
=====
BSL Racket cond (conditional) expression
=====
* ONE of several ways to BRANCH in BSL Racket!
* cond = conditional
* a multi-way branching expression!
...make ONE choice from 2 or more options
* syntax:
(cond
[bool-expr1 result-expr1]
[bool-expr2 result-expr2]
...
[else result-expr-else]
)
* the [bool-expr result-expr] parts can be called
branches or clauses of a cond expression
(yes, that includes the [else result-expr-else] also)
* semantics:
start at the first bool-expr, and see if it is
#true -- if so, its result-expr becomes the value
of the cond, and the rest are SKIPPED
else try the next bool-expr, and see if IT is #true --
if so, its result-expr becomes the value
of the cond, and the rest are SKIPPED
...and so on
BUT if you reach the else, it is always #true,
and the else's result-expr becomes the
value of the cond expression!
* NOTE:
AT MOST one branch will be "taken" --
as soon as one of the clauses' boolean expression is #true,
that clause's result-expr becomes the value of this cond.
so the order of the cond clauses MATTERS;
* CS 111 Class Style:
* start each [boolean-expr result-expr] clause in
a cond expression on its OWN line
* INDENT the start of each [boolean-expr result-expr]
clause in a cond expression under its (cond
(by at least 2 spaces)
* (for a long clause, indent its continuation line(s)
so it is clear which clause it is continuing)
* as always, ASK me if you have any questions about CS 111
class style!