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. ≥
(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.