Making a Mathematical Library

BH and Mary need to talk about Katex style. --MF, 9/12/18

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 () from (). (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?
  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.

Don't cut this commented out text about PIPE until we are all set with the pipe into in U5. --MF, 8/28/19
I'm not convinced the following works here. It's a lot of text and "talking about" how pipe works. I don't believe it's going to work as a pre-teaching for 5.3 because they are likely to forget stuff, so the question is: what does it offer here? And can we do it better? For that matter, is divisors even a particularly elucidating example of using pipe? What if we offered it in the TG and combined the stuff before this todo with the MOD stuff on the previous page? Or maybe keep the two pages separate for the sake of breathing room, but move some of the quizlets and review questions at the end of that page to the end of this page (e.g. #7-9)... --MF, 8/12/19

Nested functions (that is, functions used inside of other functions, like the way you used divisible by? and numbers inside of keep) can be hard to read. Snap! offers another way to write code with multiple functions. You can use the pipe () → () function from the "Frequency Distribution Analysis" library to write your code one function at at time.

image needs alt/title tags --MF, 6/11/19

The pipe function sends input number through numbers from (1) to () to report a list of numbers and then that list goes through keep items such that ((input number) divisible by ()?) from () to get the numbers from that list that are divisible by the input number.

Notice that the empty input slots in each function are filled by the output of the previous function (or filled by the starting data in the case of the first function):

It's like a pipe of made of pieces connected together; the input number goes in one end and works through each function, producing a new output at each step.

I find this description painful. If we're going to teach pipe at all, what's needed here is a CS-Illustrated-style picture with a specific number as input and then all the intermediate values shown with arrows pointing to which empty slot each one fills. But, why do this? Is the theory that pipe will be easier for most kids than nesting? And if so, wouldn't it be easier still to make a bunch of script variables and set each one to the result of one function call? And then show how that turns into a pipe. Or do it instead of pipe. -bh

Brian means some kind of horizontal diagram: 8 -->1, 2, 3, 4, 5, 6, 7, 8 --> 1, 2, 4, 8 --MF, 1/15/20

Pipe is a higher order function; it is a function that takes functions as input. You've seen a higher order function before: keep (in Unit 2 Lab 3).
  1. Look at the pipe version of divisors above. Talk with Your Partner What would happen if you did keep before numbers?
  2. Choose "Libraries..." in the Snap! file menu (File button), and select and import the "Frequency Distribution Analysis" library.
  3. Rebuild your number of divisors block using the pipe function.
    You can either create a new function called something like number of divisors 2 or pipe version of number of divisors, or you can rewrite the same block and just drag off the original report instruction but leave it inside the number of divisors block unattached from the hat block so will be saved in there but it won't run.

You don't ever have to use pipe again, but if you find it hard to write a function with a bunch of nested reporters, you can always import pipe again.