Building a Useful Collection of Predicates

Some tests—for example, a test to see if a number is even or if a letter is a vowel—are needed so often that you won't want to re-create them each time you need them. In this activity, you will create a set of tools that you may use often, especially for complicated programming tasks.

You've already built a predicate vowel? to test for vowels and a predicate to test for even numbers. snap has a built-in test to see if something is a number, but not a test to see if the number is an integer, and often you will want that.

  1. snap has a "greater than" operator (>), an "equal" operator (=), and a "less than" operator (<) built in, but doesn't have a "greater than or equal to" (>=) operator.

    5 >= 35 >= 53 >= 5

    Build a greater than or equal to b and test it well to make sure it does what you expect.

    creating an infix predicate

    • When you type the block's title in the Make a block dialog, choose the hexagonal predicate block shape.
    • To set the block's name >= between the two input slots, use the left plus sign in the Block Editor. left plus sign
    • You may find one or more of these Boolean operators helpful:

      and, or, not blocks

    There are several correct ways to do this! As long as your block gives the right true and false answers, it's correct.

  2. Operators like a times b that stand in between their two inputs are called infix operators. Operators like sqrt of that stand before their inputs are called prefix operators. When we write n! to mean "n factorial," the "!" is a postfix operator—it comes after its input n factorial In snap, it is possible to mix the types, as you will do in the next problem.
  3. Build a predicate that tests to see if its input is an integer. integer?-4=(with-result-true) integer?-4-point-1(with-result-false)

    You may find round a number useful.

  4. Build a block that tells whether a given number is between two other given numbers. number-between-two-others(with-result-false) number-between-two-others(with-result-true). You can decide whether "between," for your purposes, will include the two boundary numbers or not.
  5. Write a weekend? block that reports whether a given day is a weekend. Let's assume that only Saturday and Sunday are part of the weekend.
  6. Write a weekday? block. You could write this without your weekend? block, but it'll be much easier (and cleaner code!) if you use weekend? as part of this new block.