Beruflich Dokumente
Kultur Dokumente
Booleans
What
are Booleans?
Booleans are variables that can only hold one
of two values
The most common values a Boolean holds
are
True
False
Conditions
What
A
is a condition?
Types of Conditions
In
Evaluate
Evaluate
4
2
7/2
(> x 3)
For x = 4, True
For x = 2, False
For x = 7/2, True
(= (* x x) x)
For x = 4, False
For x = 2, False
For x = 7/2, False
Translate
Conditionals
What
A conditional is an expression that determines which condition out of several is true for
any given input
The general shape of a conditional expression is
(cond
[condition return-value]
...
[condition return-value])
OR
(cond
[condition return-value]
...
[else return-value])
Designing a Conditional
Inspect
Suppose
The
3 conditions are
Example:
For $900, the interest rate is 4
For $4500, the interest rate is 4.5
For $6000, the interest rate is 5
Expression:
(cond
[(<= deposit 1000) 4]
[(<= deposit 5000) 4.5]
[else 5])
Conditional Functions
Develop
Homework
An equation is a claim about numbers; a quadratic equation is a special kind of equation. All quadratic equations (in one
variable) have the following general shape:
a*x^2 + b*x + c = 0
In a specific equation, a, b and c are replaced by numbers, as in 2*x^2 + 4*x + c = 0
The variable x represents the unknown.
Depending on the value of x, the two sides of the equation evaluate to the same value. If the two sides are equal, the
claim is true; otherwise it is false. A number that makes the claim true is a solution. The first equation has one solution, 1, as we can easily check:
2*(-1^2) + 4*(-1) + (-1) = 0
The number of solutions for a quadratic equation depends on the values of a, b, and c. If the coefficient a is 0, we say the
equation is degenerate and do not consider how many solutions it has. Assuming a is not 0, the equation has
- two solutions if b^2 > 4*a*c
- one solution if b^2 = 4*a*c
- no solution if b^2 < 4*a*c.
To distinguish this case from the degenerate one, we sometimes use the phrase proper quadratic equation.
Develop the function how-many, which consumes the coefficients a, b, and c of a proper quadratic equation and
determines how many solutions the equation has:
(how-many 1 0 -1) = 2
(how-many 2 4 2) = 1
Make up additional examples. First determine the number of solutions by hand, then with DrRacket.
How would the function change if we didn't assume the equation was proper?
Hint: The discriminant of a quadratic equation is b^2 - 4*a*c
Symbols
Symbolic
Strings
Symbols (character map)
Characters
Images
Symbolic
Symbols
Racket
Since
(define (reply s)
(cond
[(symbol=? s 'GoodMorning) 'Hi]
[(symbol=? s 'HowAreYou?) 'Fine]
[(symbol=? s 'GoodAfternoon) 'INeedANap]
[(symbol=? s 'GoodEvening) 'BoyAmITired]))
Develop
Homework
Develop the function check-color. It implements a key portion of a color guessing game. One player picks
two colors for two squares; we call those targets. The other one tries to guess which color is assigned to
which square; they are guesses. The first player's response to a guess is to check the colors and to produce
one of the following answers:
'Perfect, if the first target is equal to the first guess and the second target is equal to the second guess;
'OneColorAtCorrectPosition, if the first guess is equal to the first target or the second guess is equal to the
second target;
'OneColorOccurs, if either guess is one of the two targets; and
'NothingCorrect, otherwise.
These four answers are the only answers that the first player gives. The second player is to guess the two
chosen target colors with as few guesses as possible.
The function check-color simulates the first player's checking action. It consumes four colors; for simplicity,
we assume that a color is a symbol, say, 'red. The first two arguments to check-color are "targets", the
latter two are "guesses". The function produces one of the four answers.
When the function is tested, use the teachpack to master.rtk to play the color-guessing game. The
teachpack provides the function master. Evaluate (master check-color) and choose colors with the mouse.
Structures
What
is a structure?
A structure is a collection of related data
For example, an audio track has several
pieces of related data
The
The
The
The
artist name
album name
year of release
genre
So
Structures in Racket
Pixels
Every
Let
(distance-to-0
5
(distance-to-0
5
(distance-to-0
10
(distance-to-0
13
(make-posn 0 5))
(make-posn 3 4))
(make-posn 8 6))
(make-posn 5 12))
The
Racket
Lets
Let
make-moviestar
moviestar-last
moviestar-first
moviestar-bestmovie
moviestar-sales
(define-struct
(define-struct
(define-struct
(define-struct
(define-struct
Evaluate
Homework
Define
Design
Homework
Provide