a > bis true.
sum
block, which combines two numbers using addition: maximum
(reporting the larger of two input numbers) and minimum
(reporting the smaller of two input numbers): The block takes two inputs: a list and an operation with two inputs, and it reports the result of combining all the elements in the input list using the input operation. For example, the expression below reports: 0 + 1 + 2.
You choose the operation, and combine
performs that operation by combining all the items in the input list and then reports the result.
Notice that the function used to combine the list items always has two blank input slots. The keep
block only needs one blank in its input function, but with combine
, two are required.
Combine
is a higher-order function; it is a function that takes a function as input. You've seen another higher-order function already: keep
(in Unit 2 Lab 3).
Unlike keep
, the combine
function is only used with a few input functions.
Which functions?
Combine
is mostly used with only these functions:
and the and blocks you've just written yourself.
combine
to build maximum of list
and minimum of list
.sum of list
block that reports the sum of the items of a list.average of list
block that calculates the average of the items in a list:average of list
First think: how do you calculate an average? Then think: what blocks could help you do that in Snap!?
Average of list
and sum of list
are procedures that you'll need repeatedly, so after a while, you just know how to write them, or you'll put them in a library, and you won't have to reinvent them each time. Finding the maximum or minimum value in a list and checking for divisibility are also commonly needed algorithms.
RETURN(like
report
). For example, this definitionIn many languages (including Snap!) variables must be declared in some way (in Snap!, you can click the "Make a variable" button or use the script variables
block). But the AP's language doesn't include variable declarations, so you won't see them on the exam.
PROCEDURE squareRoots(number) { positiveRoot ← sqrt(number) RETURN([positiveRoot, -1 * positiveRoot]) }
report
in Snap!, when a RETURNstatement is executed, the flow of control returns to the place in your code where the procedure was called, and the procedure returns the value of the expression inside the
RETURNcommand or
report
block.sqrtisn't built in to the AP's language so it is written in lower case like other programmer-defined procedures.
IF(a > b) { RETURN(true) } ELSE { RETURN(a = b) }
a > bis true, the code in first block of statements runs; if it is false, the code in second block of statements runs.
When will "Error. Invalid entry." be displayed?
PROCEDURE greaterThanOrEqualTo(a, b) IF(a > b) { RETURN(true) } ELSE { RETURN(a = b) } DISPLAY("Error. Invalid entry.") }
a > bis true.
RETURNdoes. Like
report
in Snap!, RETURNends the procedure. Once it is run, no other code in that procedure is run, and the flow of control returns to the place where the procedure was called.
a > bis false.
RETURNdoes. Like
report
in Snap!, RETURNends the procedure. Once it is run, no other code in that procedure is run, and the flow of control returns to the place where the procedure was called.
RETURNdoes. Like
report
in Snap!, RETURNends the procedure. Once it is run, no other code in that procedure is run, and the flow of control returns to the place where the procedure was called.
greatest common divisor
block. (Use your block.)greatest common divisor
maximum of list
as well as divisors
and also to build an intersection
block that takes two lists as input and reports all of the items that are on both lists.greatest common divisor