Making a Mathematical Library

In this project, you will create a library (a collection) of math tools that builds on the blocks you created in Lab 3 Page 2: Combining Predicates.
AAP-2.M.3, AAP-3.D.3
AAP-3.D.1

A software library is a collection of procedures that can be used in programs.

Using libraries simplifies the development of new programs. When you use procedures that you already know work correctly, you reduce the amount of time you need to spend coding, the number of possible bugs your code can have, and how much of your project you need to test.

  1. Export the mathematical blocks from your U2L3-Predicates project.

    Exporting Blocks

    1. Open the Snap! project from which you want to export blocks. (In this case, your U2L3-Predicates project.)
    2. Choose "Export blocks..." from the Snap! file menu (File menu button).
    3. Select only the block(s) you wish to export. (In this case, you want is () between () and ()?, less than or equal, greater than or equal, and () not equal ().)
      You can either deselect the blocks you don't need exported, or you can right-click the background, choose "none," and then select only the ones you want.
    4. Snap! libraries and Snap! projects are both exported as XML files.
    5. Click "OK." An XML file will download.
  2. Import these blocks into your U2L4-MathLibrary project, test them and resolve any issues, and save.

    Importing Blocks

    1. Open the Snap! project into which you want to import blocks. (In this case, your U2L4-MathLibrary project.)
    2. Import the XML file by dragging the XML file from your downloads folder into the Snap! window.
      You can also import the XML file by choose "Import..." from the Snap! file menu, locating the XML file on your computer, and clicking "Open."
    3. Test the blocks that you imported:
      1. Find the imported block(s) at the end of the palettes that contain them.
      2. Click each imported block to make sure it runs properly.
You are creating a math library. Snap! has libraries for many things, such as one for accessing different parts of words and sentences...
Snap! File menu opened with cursor over 'Libraries' Snap! 'Import library' dialog box highlighting 'Words, sentences' library

Debugging Dependencies

even? (number#){report(Obsolete!)}

You created your even? block using another custom block, divisible by?. If you want to use even? in another project one day, you must export both blocks in order for even? to work properly.

If you ever see this red Obsolete! block in code you have imported, it means that a required block was not exported. You'll have to go back to the original project and export again being sure to select all of the blocks needed by the blocks you want.

  1. AAP-2.M parts a and b, AAP-3.D
    Copy and modify the code from your even? predicate to develop an odd? predicate for your library.
    odd? (6) reporting false odd? (-7) reporting true
AAP-2.M.1, AAP-3.D.2

You can create algorithms from scratch or by combining or modifying existing algorithms. When you are thinking of using existing code, you can either use your own code or use code from someone else's library.

  1. AAP-2.M parts a and b
    Develop a number of divisors reporter with numeric input slot block (using keep), and use it to answer these questions:
    • What kinds of numbers have an odd number of divisors?
    • What kinds of numbers have exactly two divisors?

    You can do this on your own or follow these steps.
    1. Build a divisors block using keep.
      divisors of (9) reporting {1, 3, 9} divisors of (12) reporting {1, 2, 3, 4, 6, 12}
      Hints about building the divisors block
      • You could build this with for or for each, but it will require less code to build it with keep items 'predicate input slot' from 'list input slot'. (You learned about keep on Unit 2 Lab 3 Page 5: Keeping Items from a List.)
      • You could build this with mod, but you already built () divisible by () ? (using mod), so divisible by? will be simpler to use.
      • The numbers from () to () block may be helpful.
    2. Build a number of divisors block.
      number of divisors (9) reporting 3 number of divisors (12) reporting 6
    3. Build an expression using keep to answer each of these questions that were introduced above:
      • What kinds of numbers have an odd number of divisors?
      • What kinds of numbers have exactly two divisors?
Save your work
  1. Talk with Your Partner For each of the two questions about divisors above, determine: why do these kinds of numbers have these kinds of divisors?

You've now made a small library, and you might want to write a list of instructions for how to use the functions in your library (for example, divisors of takes a positive integer as input and reports a list of numbers). The instructions form an Application Program Interface for the library.

: APIs
AAP-3.D.4, AAP-3.D.5

An application program interface (API) documents what a programmer needs to know about using a library: it's a description of each procedure's purpose, inputs, and outputs (but not its algorithms).

What is a Web API?

A common kind of API is a web API in which the library exists on someone else's computer. For example, the Google Maps API describes how to embed a Google Map on your own website.

I'm actually inclined to cut all the rest of this yellow box. --MF, 1/15/20

A web API is just a call to a procedure on another machine. For example, these are three different notations for the same procedure call, which looks up the number of searches for "BJC" in the US:

The only difference is that the URL shows where on the Internet to find the procedure.