Page 2: Combining Predicates

Unit 2, Lab 3, Page 2

On this page, you will combine predicates using Boolean functions to write on the stage in three colors.

Boolean Functions

At the very lowest level, computer circuitry is made of wires, and each wire is either on or off. So the only operations that can be performed at that lowest level are those that operate on single-bit values (just ones and zeros, that is, just ons and offs). These are called logical (or Boolean) functions. (They’re predicates, because their range is Booleans, but these are special in that their domain is also Booleans.) There are three Boolean functions in the Operators palette:
and, or, not blocks

Notice that both the blocks themselves and the input slots in the blocks are hexagonal. Boolean functions take Boolean values (True or False) as inputs and report a new Boolean value as output. When you use Boolean functions in a program, though, the inputs will usually be expressions such as variable = 3.

  1. Experiment with different input values in all three blocks, and take notes about the results you get.

    Instead of dragging a true or false block into the input slot of an and, or, or not block, you can click the empty input slot to control a true/false toggle: (true) and (false)

The and and not blocks work exactly the way you’d expect from the meanings of those words in English:

  • (true) and (true) reports true, and any other combination of inputs reports false.
  • not () reports the opposite of whichever input value you use.

But or is a little different in computer science. In English, the word “or” has two different meanings:

  • Either but not both: such as “Let’s check whether it will be rainy or sunny tomorrow.” In computer science, this is called exclusive or because one option excludes the other.
  • Either or both: such as “Ask your teacher if you have any questions or if you need help.” This is called inclusive or because it’s possible for both options to be included.
  1. Experiment. Does the () or () block implement exclusive or inclusive or?

The () and (), () or (), and not () blocks will appear as

AND

,

OR

, and

NOT

and will work exactly the same way as they do in Snap!.

  1. Make the blocks less than or equal (which reports true if the first number is less than or equal to the second number, and false otherwise), greater than or equal (which reports true if the first number is greater than or equal to the second number, and false otherwise), and () not equal (). (You can copy the characters ≤, ≥, and ≠ from this page and paste them into the Snap! Make-a-block window.)

    So these

  2. Build a predicate that tells whether an input is between two other inputs, and test it with several different cases.

    You can decide whether between? will include the two boundary inputs or not.

    is (3.8) between (3.9) and (4.7) ? reporting false is (apples) between (alphabet) and (azure) ? reporting true

Making a Predicate

  • Choose the hexagonal predicate shape.

    Make a block dialog highlighting the predicate button

  • You must use the report() block to report the result of reporters (including predicates).

:

  1. Use between? to create a script that lets you write on the stage in three colors (depending on the height on the stage), using your mouse.
  1. Because there are only two Boolean values (true and false), there are only four combinations of inputs for a two-input Boolean operator. Here are the four combinations shown with the or operator:
    or (true) (true)
    or (true) (false)
    or (false) (true)
    or (false) (false)

    1. With or, the four combinations report true, true, true, and false in that order. (Check for yourself that this is true.) So, you can represent the or function as TTTF (using T and F as abbreviations for true and false). What are the four letters for the and function? (Assume the same ordering of the inputs: TT, TF, FT, and FF as shown above with or.)
    2. Make up another set of four letters (such as TFTF), and build a predicate function with that behavior. Describe how the function behaves.

      Click to see an example showing and describing the behavior of TFTF.

      (true) TFTF (true) reporting true
      (true) TFTF (false) reporting true
      (false) TFTF (true) reporting true
      (false) TFTF (false) reporting false The TFTF function reports true whenever the second input is true no matter what the first input is. It’s not a useful function because you could just use the second input as the predicate instead of building this predicate function.

    3. Do the same thing with another set of four letters.
    4. How many possible two-input Boolean operators are there?
    5. Talk with Your PartnerObviously, TTTF (the or function) is useful, but TTTT is not useful (why not?). Which of the possible two-input Boolean operators do you think would be useful? Discuss why.
    6. How many three-input Boolean operators are there?