Creating Predicates

  1. Analyzing and debugging: These two blocks are quite similar. Build them both. Which one works properly?

    Block title Even Or Odd, input n; if (n mod 2 equals 0) [say (join n with text is even) for 1 second]; say (join n with text is odd) for 1 second Block title even or odd, input n; if (n mod 2 equals 0) [say (join n with text is even) for 1 second]  else [say  (join n with text is odd) for 1 second]

There are different traditions on how to name predicates. Using question marks in the name, as in even?, is hard to miss, but some people don't like it because in English the question mark would go at the end: is (n) even?. And in any case English questions don't go after the word if. But don't try too hard to read or write any programming language as if it were English.

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.

  1. Create the even? (n) block.
    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.
    The video below shows almost all you need to do to define the even predicate, but an important step is missing.
    • You must define your block to be a predicate rather than a command.
    • You must use the report block to report the result.
  2. What else must be done before clicking OK?
  3. Test your new block to make sure it works properly.
  4. defining a predicate block

  5. Edit even or odd (from problem 1) to make it use the the even predicate block. As always, test the result to be sure it works.
    if the number is even, run these steps, else run some other steps
  6. 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.

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

    Again, the block is defined with the hexagonal predicate shape because it outputs only true or false. Choose the shape when you first define the block.

    make a predicate block by choosing the correct shape

    vowel definition

    GitHub Feedback: The vowel block doesn't have much context. I found out that it relates to the Take It Further section for words that end in y, but there should be a link or description of some sort added to clarify this.
  7. What is an indefinite article? Look it up! Here's a video:There are three articles in English: a , an, the
    Now, define a block use indefinite article (noun) that works like this:
    use indefinite article animation showing (a lion) and (an elephant)
    make a reporter block When you define this block, you will need to choose the rounded reporter shape.

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

The solution to problem 1 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 problem 4 was approached above used several if statements because it tested for several conditions.

  1. Talk with Your Partner
    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 problems 7 and 8, 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 whatever = 0, you used direction mod whatever = 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?
  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"

 
  1. Problems 7 and 8 can be solved with conditionals or without. Whichever way you solved it, figure out how to solve it the other way.
  2. Challenge: In problem 8, 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

Take it even further!

    Data types: snap lets you manipulate a variety of data types. You have already used blocks like letter (1) of (world) to manipulate strings of characters such as "New York" or "Another hour has passed". You have also manipulated numbers.

    The next two problems give you a first taste of a new data type, lists, and show how useful lists can be. In Unit 3, you will study lists in depth.

  1. Instead of using many if statements in vowel?, you can make a list of vowels and then test to see if a particular letter is in that list. snap makes that a snap!
    1. Construct list (a e i o u), click on it, and see what it does.
    2. The just the list a e i o u that you see when you click on list (a e i o u) is an artist's way of picturing a list inside the computer. The image numbers each item of the list and tells you how long the list is. The scroll bar lets you see list elements that aren't shown in the small picture.
    3. Experiment with these blocks to see what they do.
      To save work, just duplicate list (a e i o u) a few times to use in each experiment.

      list (a e i o u) list a e i o u contains m list a e i o u contains e list of vowels contains randomly picked letter of pencil item pick random element from list of I you he she it we they

    4. Challenge: Redefine vowel? using list and contains.
  2. Analyzing, and programming elegance: Here are two ways you might use list-(a-e-i-o-u)-contains (some letter) to redefine vowel?. With your pair programmer, figure out how each works. Decide which you find clearer.
    is letter a vowel (v4) is letter a vowel (v3)
  3. Problem 1, at the top of the page, seemed to suggest that to choose between alternative actions, you must use if-else, not just if. Problem 4, however, seems to choose alternatives with plain if. Why does that work? (There's a subtle hint in the shapes of the say and report blocks.)