Students build various predicates (examples below) and use them with keep
to perform various queries on a list of words to solve a crossword puzzle.
Predicates are reporters that report only Boolean values: true
or false
. Students have used predicates with conditionals such as if
and repeat until
.
Students learn to build new predicates by combining basic predicates (like =
or >
) with operations like length of
or letter 1 of
, or the mod
block (which reports the remainder when a number is divided by another).
Then they learn to combine their predicates into more complex predicate expressions using Boolean operators (and
, or
, and not
), so that they can select list items that meet multiple criteria.
Keep
ing Items from a Listand
, or
, and not
).≤
, ≥
, ≠
, and between?
.≥
).or
is inclusive (if either or both of the inputs are true, the result is true), and a separate function, xor (pronounced x-or, for "exclusive or"), is used when two true inputs must result in a false output. Exclusive or is not included in Snap! and is not taught in BJC. Only two (and
and or
) of the 16 possible Boolean operators are included. Interested students may wish to do additional research on the other Boolean operators (such as nand meaning not (and)
; xnor meaning not (
xor)
, etc.). +
. Snap! uses the term loosely, so not everything in the "Operators" palette is a two-input function. So, technically, not
isn't a operator. and
function can be represented as TFFF; ii-v) Answers will vary for parts ii and iii. The following table lists the 16 (part iv) possible two-input Boolean operators and their usefulness. The goal is for students to think about usefulness—not for them to accurately categorize each of the functions or even to learn the logic of each one. Unless you have a background in logic or computer science, even you (as teacher with the answer key) are likely to find some of these take a bit of thinking to wrap your mind around.
Useful? | Why? | |
---|---|---|
TTTT | no | always reports true |
TTTF | yes | equivalent to or |
TTFT | yes | equivalent to converse implication (false only when the first input is false but the second input is true; the second input being true implies the first input must be true) |
TTFF | no | reports the value of the first input regardless of the second input, so you could just use the first input as the predicate instead of building this predicate function |
TFTT | yes | equivalent to implication (false only when the first input is true but the second input is false; the first input being true implies the second input must be true) |
TFTF | no | reports the value of the second input regardless of the first input, so you could just use the second input as the predicate instead of building this predicate function |
TFFT | yes | checks whether two inputs are the same (equivalent to = and to XNOR, exclusive NOT OR) |
TFFF | yes | equivalent to and |
FTTT | yes | equivalent to NAND (that is, not (and) ) |
FTTF | yes | checks whether two inputs are the different (equivalent to not(=) and to XOR, exclusive OR) |
FTFT | no | reports the value of the opposite of the second input regardless of the first input, so you could just use the opposite of the second input as the predicate instead of building this predicate function |
FTFF | yes | equivalent to nonimplication (true only when the first input is true but the second input is false; the first input being true implies the second input must be false) |
FFTT | no | reports the value of the opposite of the first input regardless of the second input, so you could just use the opposite of the first input as the predicate instead of building this predicate function |
FFTF | yes | equivalent to converse nonimplication (true only when the first input is false but the second input is true; the second input being true implies the first input must be false) |
FFFT | yes | equivalent to NOR (that is, not(or) ) |
FFFF | no | always reports false |
≥
(shown below)? The goal of this discussion is for students to see that a good optimization is that you never need to program if some predicate report (true) else report (false)
, you can just do report (some predicate)
.and
, or
, and not
.x coordinate
goes left to right and is positive to the right of center and that the y coordinate
goes up and down and is positive above the center. It's not important to know the precise dimensions of the stage.and
block in the second problem were replaced with or
.((x position) ^ 2)
in it.Keep
ing Items from a List.
keep
block uses predicates to select items from a list.keep
.keep
block: It's a reporter that takes a predicate and a list as input, and it reports only the items from the list that meet the criteria specified by the predicate. The predicate itself must have a blank for the list items to cycle through.=, ≠, >, <, ≥, and ≤.
a = b
a ≠ b
a > b
a < b
a ≥ b
a ≤ b
a = bevaluates to
trueif
aand
bare equal; otherwise, it evaluates to
false.
NOT,
AND, and
OR, which evaluate to a Boolean value.
NOT condition, which evaluates to
trueif
conditionis
false; otherwise it evaluates to
false.
condition1 AND condition2, which evaluates to
trueif both
condition1and
condition2are
true; otherwise it evaluates to
false.
condition1 OR condition2, which evaluates to
trueif
condition1is
trueor if
condition2is
trueor if both
condition1and
condition2are true; otherwise it evaluates to
false.
trueor
false.