ST EK List:
4.1.1C Selection uses a Boolean condition to determine which of two parts of an algorithm is used.
5.5.1E Logical concepts and Boolean algebra are fundamental to programming.
5.5.1F Compound expressions using and, or, and not are part of most programming languages.

What's a Predicate?

Ruthless Suggestions from Al

I want to rewrite this page to bring back a simplified version of my number guessing game with more predicates. They'll build these same predicates but for the context (e.g. project) of letting the player ask more than just greater/less than. (If not this, then I think we should start keep earlier on the page.) --MF

**ST-Some issues with the Guessing Game: "say" blocks need to be in an "if else" and "goto front" must be employed to avoid overlaps.**

Is this idea still relevant? --MF, 11/19/17

Need to add EK "5.5.1G Intuitive and formal reasoning about program components using Boolean concepts helps in developing correct programs." to pages 2.3.1 and 5.3.3. --MF, 12/1/17

In this lab, you will develop a word puzzle solver that will search through a long list of words and report words with specific characteristics.

On this page, you will review predicates and build a few that you can use in other projects.

Predicates

Predicates are reporter blocks (functions) that always report a Boolean value (they report only the values true or false). In Snap!, they are represented by hexagonal blocks.

You have seen predicates before. Predicates compute the condition used by conditionals (such as if or repeat until) to decide when to do something. Predicates ask a question such as "Does the player list contain the player's answer?" or "Is this sprite touching the sprite called 'Sprite'?"
greet player skeleton sprite following sprite code

  1. "U2L3-Predicates"Create a new project called U2L3-Predicates
  2. There is a commented-out hint box here. --MF, 11/19/17
    In the last lab (and also way back in Unit 1 Lab 5), you made scripts to get the sprite to follow the mouse.
    go to x: (mouse x) y: (mouse y)
    Now create a script that lets you use your mouse to write on the stage in two colors depending on the mouse's position on the stage:
    Use one of these equality/inequality predicates:
    less than, equal to, and greater than predicate blocks
    using the mouse to write 'mouse' on the stage
  1. Improve your drawing script so that the sprite always follows the mouse, but draws only when the mouse button is down, so that you can draw disconnected shapes.
    Uncheck the "draggable" box above the scripting area before you try this (so that Snap! doesn't think you are trying to drag the sprite when you click).
    draggable box checked
    You'll probably want to use the mouse down? predicate block block, which you can find in the Sensing palette.
    bicolor printed hello
  1. You may find one or more of these Boolean operators helpful for between?.
    and, or, not blocks

    You can decide whether between? will include the two boundary numbers or not. And you can change it later depending on where you use it.

    Build a predicate that tells whether an input number is between two other numbers, and test it with several different cases.
    (3.8) between? (3.9) and (4.7) reporting false (4) between? (3.9) and (4.7) 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 reporter functions (including predicate functions).
    • Many languages (and the AP CS Principles Exam) use return instead of report as the name of the command to give a value back at the end of a function call.

  2. 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:
    Your letters will be connected unless you had time to program the disconnected drawing version above.
    using the mouse to write in three colors on the stage
  3. Build a predicate that tests for divisibility by following the steps below.
    (15) divisible by (3) ? reporting true (15) divisible by (6) ? reporting false
    1. First, experiment with the mod block.
      1. Try various inputs.
      2. Keep the second number constant, and try various inputs for the first number.
      3. Form a hypothesis. What do you notice?
    2. The () mod () block reports the remainder when the first input is divided by the second. For example, (17) mod (5) reports 2 because when 17 is divided by 5, the remainder is 2. When one number divides another evenly, the remainder is 0. So, (15) mod (5) reports 0.

    3. Now, define the () divisible by () ? predicate using mod.
  4. Use your divisible predicate to build a predicate that tests whether its input is even (divisible by 2).
    even? (-22) reporting true even? (7) reporting false Now Is a Good Time to Save
    In this picture, the even and odd numbered rows are different. In the next lab, you will use your even? block to draw a brick wall.
    Sample image of brick wall
  1. Build a predicate that tests whether its input is an integer. You may find round () useful.
    integer? (4) reporting true integer? (4.1) reporting false

Self-Check: Predicates

  1. These questions are similar to those you will see on the AP CSP exam.
    There's no built in ≤ block in Snap!. Suppose we wanted to build one. Which two of the following Boolean expressions are equivalent to the expression num less than or equal to 23 predicate?
    (num < 23) and (num = 23)
    (num < 23) or (num = 23)
    not (num > 23)
    not (num > 22)
    The following code fragment was designed to test if number is odd:
    IF (MISSING CONDITION)
    {
      DISPLAY "This number is odd."
    }

    if (missing condition) say [This number is odd.]
    Which of the following can be used in place of the MISSING CONDITION?
    ((number) mod (1)) = (0)
    ((number) mod (1)) = (1)
    ((number) mod (2)) = (0)
    ((number) mod (2)) = (1)