Monday, October 6, 2014

Customized Blocks and Lists

From csp4hs - summer 2014 session

Lesson Objectives

Thus far, we have used Snap! blocks that were already existing and provided to us as part of the core Snap! language. There are many situations that may arise when a programmer needs to define their own blocks to support customized features that their program needs. The ability to customize blocks is very important to support the concept of software reuse. We do not want to reinvent the wheel each time we need a special feature in our program - creating a customized block allows us to use the block in many different contexts, saving us time and also helping to make our programs more correct (i.e., we can put extra effort in making sure a block is correct, and then reuse the block in many other contexts). In this lesson, we will learn how to make our own blocks in Snap!
Another topic covered in this lesson is an introduction to a basic data structure known as a list. In Snap! and other programming languages, a list can be used to store a collection of common values in a single variable name. For example, if a program needed to store the grades for a class of 20 students, rather then have 20 separate variables for each student, a list allows us to create a single variable name that contains all of the grades together. We also learn in this lesson how to create and manipulate lists in Snap!, and consider several contexts where lists may be used.
All of the example programs shown in the primary presentations are also coded in other videos that you can watch below. Make sure that you do not just watch the videos - try to program the examples yourself in Snap! by following along with the video screen shot.

Lesson Tasks

Please consider the following sequence of topics for this lesson on blocks and lists:
Building Your Own Blocks
  1. Watch the video below entitled "Build Your Own Blocks." The link to the slides is just above the video.
  2. Consider our original starting point in earlier lessons of this unit that focused on simply drawing a square. The code to draw a square served the purpose of illustrating basic Snap! commands (e.g., move), but this code was not very useful because it assumed a specific type of shape with a fixed length of sides. This lesson progresses through a series of generalizations where the core square code is made more reusable by adding parameters to the block that aid in specialization. For example, the fixed length of the square is removed such that the caller of the block can specify a specific length. By abstracting out the length of the side of the square, we can now reuse the block to draw squares of any size. However, drawing a square in itself is not so interesting. The next progression in this lesson is to abstract the number of sides, which will create a more general drawShape block. The caller of drawShape can specify not only the length of sides, but also the number of sides. This allows a very general block that can draw everything from a triangle to a circle.
  3. Watch the video entitled "From square to drawShape" below, which shows how blocks are created in Snap! When teaching this lesson, it is very important to draw your student's attention to the importance of procedural abstraction and how it can allow us to create more general blocks that can be reused in many contexts (e.g., drawShape has many more options for reuse than a square block of a fixed size - there are just not many places where a fixed square would need to be reused compared to a more general drawShape). The concept of procedural abstraction allows programmers to create libraries of software that can be reused in future contexts that may not have even been anticipated when the block was designed initially. When introducing this to your students, guide them through the same progression of a very fixed program (the first square) through the drawShape block, pointing out along the way how each new evolution of the block can support a larger number of contexts. Help them to understand how a block can be generalized by abstracting the essential parts of a shape (i.e., its length and number of sides).
  4. Make sure that you understand the power of procedural abstraction and how this can be implemented in Snap! with customized blocks. You should also understand the different types of blocks: Command blocks just perform an action and do not return a result (like drawShape); Reporter blocks return a value (sort of like a mathematical function); and Predicate blocks are like Reporters, except the return type is limited to a Boolean value (i.e., they only return true or false). Blocks can also receive parameters, which provide input to a block during its computation. Parameters can be of different types, as shown in the slides and videos below. Parameters can be placed in post-fix notation (e.g., the length and number of sides appearing after drawShape) or in-fix notation (e.g., the appearance of the "<=" operator between the parameters, as shown in a video below). Watch the video entitled "Max and Less-Equal," which shows how a Reporter (that is post-fix) and Predicate (that is in-fix) are created, respectively.


No comments:

Post a Comment