Creating and Using Predicates

NOTE: There were numbering issues on this page at one point. This has been fixed, so the references below may not be correct. --MF Notes from Al on U2L1P2–6:
From Observation Notes Analysis:

To make progress with Tic Tac Toe, your program needs to control which action happens at which time. Control blocks are the topic of this page. You will also start a new mini-project about English words and simple phrases that will give you practice with conditional statements and with breaking complex tasks into meaningful parts.

  1. "U2L1-Predicates"Create a new project called U2L1-Predicates
  2. You know that the code if-n-mod-2-equals-0 means "if n is even," but programs are much easier to read and debug when the code says what it means, something like if even? n or if n is even.
  3. The video below shows almost all you need to do to define the even predicate, but an important step is missing.
    • You must choose the hexagonal shape to define your block as a predicate rather than a command.
    • You must use the report block to report the result.
    • What else must be done before clicking OK?
    • The video also shows a different (optional) way to create an input name. You can, if you like, type the title text and input name at the same time, and then change the part that you want to use as an input name by clicking on it and choosing "input name." Use whatever way you like best.

    defining a predicate block

  4. Test your new block to make sure it works properly.
  5. use-a-or-an-with-(word)

    To create realistic dialogues, a program needs to adjust some words to fit others. For example, it needs to know when to use a or an. A peach or an peach? A apple or an apple?

    To decide, it needs a predicate that tests the next letter to see if it's a vowel. It should work like this:

    predicate vowel? tests e and reports true, and tests b and reports false

    There are many ways to create a block like this. Here's one way, started for you.

    Because vowel? outputs only true or false, remember to choose the hexagonal predicate shape when you first define the block.

    make a predicate block by choosing the correct shape

    Complete it, test it, and save it for future use.

    vowel definition

  6. When you define these blocks, you will need to choose the rounded reporter shape.
    make a reporter block
    Use vowel(letter(1)of to define a block use "a" or "an" with (noun), that takes a word as input and reports either "a" or "an."
  7. Now, use that block you just created to define a block join "a" or "an" with (noun) that works like this:
    join-a-or-an-with

    You will need to use join words with two inputs. The block automatically shows a slot for only one input, but you can use the arrows input arrows to increase or decrease the number of inputs.

  8. Now Is a Good Time to Save
Please do not reference problem numbers. These (below) were problem number references, and I had to guess what they were referencing because the problems had clearly been renumbered. --MF

Pair Programming Swap The definition of even? needs only one if statement, because all integers are either even or odd. If the boolean expression (n mod 2 = 0) evaluates to false, the number is not even, so it must be odd.

The way vowel? was approached above used several if statements because it tested for several conditions.

  1. Talk with Your Partner
    The expression n mod m reports the remainder when n is divided by m. That remainder is 0 only when n is a multiple of m. So, 127 mod 60 reports 0 only when the sprite's direction is a multiple of 60°. So, the predicate direction-mod-60-equals-0 is true only when direction is 0, 60, 120, 180, 240, or 300.

    When the sprite's direction is not a multiple of 60, 127 mod 60 reports a number other than 0. For example, if the sprite's direction is 127°, 127 mod 60 reports 7.

    127 divided by 60 leaving a remainder of 7

    When the sprite is pointing in direction 90, direction mod 30 reports 0.

    90 divided by 30 with zero remainder

    In how many other directions will the predicate direction-mod-30-equals-0 report true?

    Analyzing: To solve the puzzles in the next two problems, you will need to understand how the conditionals (if else and repeat until) control each of these scripts. Construct and analyze them with your pair programmer, experimenting with the conditions until you are sure you can explain exactly how this code does what it does.

    If Direction Mod 60 equals 0 set Pen Size to 5 Else Set Pen Size to 1 Script

    1. What if, instead of (direction) mod ( ) = 0, you used (direction) mod ( ) = 20?
    2. What if you tried (direction) mod (23) = 0?
    3. What if you use turn 2 degrees or turn 7 degrees instead of turn 1?
    4. What if you use turn left ( ) degrees instead of turn right ( ) degrees?
  2. Two puzzles: For each picture, create a script (or modify one you already have) that keeps the sprite moving in a circle drawing that picture.
  3. Puzzle 5: thin green circumference with four thick red dots at north, west, south, and east, sprite facing counterclockwise Puzzle 6: thin blue circumference with two very thick red dots in southeast at roughly four-o'clock and five-o'clock positions, sprite circling clockwise

  4. And try to create a script that has this behavior.
  5. Clock animation: draws circle with dots for each number and says 'another hour has passed'

Now Is a Good Time to Save
 
  1. The last two problems above can be solved with conditionals or without. Whichever way you solved it, figure out how to solve it the other way.
  2. Challenge: In the last For You To Do problem, the sprite always says "Another hour has passed." You can do better than that: Make it say what time it has just reached, like this.
  3. animated clock that says the hour