Page 2: Making a Mathematical Library
Unit 2, Lab 4, Page 2
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
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.
-
Export the mathematical blocks from your U2L3-Predicates project.
Exporting Blocks
- Open the Snap! project from which you want to export blocks. (In this case, your U2L3-Predicates project.)
- Choose “Export blocks…” from the Snap! file menu ().
-
Select only the block(s) you wish to export. (In this case, you want , , , and .)
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.
- Click “OK.” An XML file will download.
Snap! libraries and Snap! projects are both exported as XML files.
-
Import these blocks into your U2L4-MathLibrary project, test them and resolve any issues, and save.
Importing Blocks
- Open the Snap! project into which you want to import blocks. (In this case, your U2L4-MathLibrary project.)
-
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.”
-
Test the blocks that you imported:
- Find the imported block(s) at the end of the palettes that contain them.
- 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…
Debugging Dependencies
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.
-
AAP-2.M parts a and b, AAP-3.D
Copy and modify the code from your
even?
predicate to develop anodd?
predicate for your library.
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.
-
AAP-2.M parts a and b
Develop a 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.-
Build a
divisors
block usingkeep
.
Hints about building the
divisors
block-
You could build this with
for
orfor each
, but it will require less code to build it with . (You learned aboutkeep
on Unit 2 Lab 3 Page 5:Keep
ing Items from a List.) -
You could build this with
mod
, but you already built (usingmod
), sodivisible by?
will be simpler to use. - The block may be helpful.
-
You could build this with
-
Build a
number of divisors
block.
-
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?
- 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).
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:
- https://trends.google.com/trends/explore?q=BJC&geo=US
-
EXPLORE (q = BJC, geo = US)
The only difference is that the URL shows where on the Internet to find the procedure.