Monday, October 6, 2014

List Applications

List Applications
  1. Watch the video below entitled "List Applications." The link to the slides is just above the video.
  2. There are many application areas where lists can be beneficial. A short example program that plays a short song/jingle is created in this lesson (watch the video "Playing a song with two parallel lists"). Can you identify this song (be the first to post to Piazza - but please check whether someone else has already posted)? This program creates a block called playSong that receives two parameters as input - a list of notes, and a corresponding list representing the duration of each note. The meaning is that the note found in a particular index of the list "notes" is played by the duration indicated using the same index into the "duration" list (e.g., when index is 2, note 65 will be played for a duration of 0.15 seconds). The playSong block is called twice with the same parameters with a brief rest between each iteration. Study the code in the video and on slide 2 of "List Applications" - notice how the index loops over the length of the notes, and how the "play note" block is called by retrieving both the note and the duration from the two parallel lists. If you would like to understand more about how to play notes in Snap!, please check out the following web page (suggested by Bucky Garner) that lists the note values.
  3. Snap! provides a pre-existing block called "contains" that will return a Boolean value based on whether a certain item is contained within a specific list. However, in some cases, it may be useful to know the specific location in the list where an item appears, beyond just whether it exists in the list (e.g., it may be helpful to know that a specific student name is found at index 3 in a studentNames list to allow for further processing of that student). Watch the video below entitled "Searching for the location of an item in a list." Study the code that appears in this video (and on slide 4 of the presentation) and observe the check for the item in the loop using the "if" statement. Also, notice how the value of the index is reported back to the caller when the item is found, or else reported back as -1 when all of the list items have been exhausted without a successful search. Try to follow the logic of this code - ask questions on Piazza or Hangouts if this is not clear to you how it works.


Planning Ahead 

 Reusable software is very important to improve productivity and even correctness of software. Procedural abstraction allows us to create our own blocks that are generalized for many different contexts of usage. This lesson explored the benefits of customized blocks and also introduced the concept of lists. Make sure that you study the various list examples in this lesson and notice the pattern that is often used for iterating over a list (see point 4 above in the "Lists" section that mentions the parts of this pattern).

 The next lesson will be focus on two case studies that help guide you into creating more detailed programs than what we have seen in the past lessons. You will create a maze game through a guided video, and then you will be asked to create a version of Snappy Bird by following the instructions on a 30-page tutorial. Before you move forward to the next lesson, please make sure that you understand the general programming concepts previously introduced in the lessons of Unit 3.

Lists

From csp4hs - summer 2014

Lists

  1. Watch the video below entitled "Lists." The link to the slides is just above the video.
  2. As noted in the last lesson, variables are used to store the values of computations that are made in a program. Our past look at variables considered only a single variable that contained a single value. There are many occasions when a programmer may want to group a set of values into a single variable name (e.g., a list of class grades, a sequence of notes for a song, the list of high scores for a game), rather than storing each value separately (e.g., grade1, grade2, ..., grade20). If we had to store each value in a separate variable, our programs would become complex (imagine over 1000 different variables to store the notes of a song). A data structure allows us to group information together into a single variable that can be created and manipulated. Lists are a specific type of data structure in Snap! that allow a programmer to store a sequence of values that can be individually indexed through a common variable name. In Snap!, each element in a list is indexed, starting at index location 1. For example, on slide 2 of the "Lists" presentation below, index location 1 is an "Apple" and index location 5 is "Grape." The index locations of -1 and 6 are undefined in the example on slide 2.
  3. Go to Snap! and look at the Variables palette, making note of the various blocks that can process lists (see slide 3 of the presentation below). These blocks can be used to create, delete, add, insert at specific location, determine the length of a lists, and access values from a list. Using a loop, it is possible to iterate over all of the elements of a list and perform some special computation.
  4. Watch the video "Printing the contents of a list" located below. Notice that there are three key parts to this program, which you will often find in most code that processes a list: 1) Creation and initialization of a loop counter (often called "index" or "counter"), 2) a repeat loop that iterates over the length of a list, 3) some access to the list through blocks like "item of", and 4) within the loop, an update to the loop counter to move to the next element in the list (often done with the "change" block in Snap!). Study the program that says "C S P 4 H S" and make sure that you understand the list blocks that are used and how they work in conjunction with the repeat loop.
  5. Watch the video "Summing a list of numbers" located below. This program creates a new Reporter block (called sum) that receives a list of integers as input and returns the sum of all the numbers in the list. The benefit of using a list is that sum can be called with an arbitrary number of integers - it can compute the sum of 10 numbers as easily as it can computer the sum of 100 numbers. There is nothing fixed in the block definition that ties it to a specific size of the list (the use of the length block enables this benefit). In the code of the block, in addition to the loop index counter, there is also a variable called tempSum that is used to keep track of the running sum of the list numbers. Make sure you understand how this block works by tracing out a sample list input and how the values of index and tempSum change during the iteration over the list. After the sum of the list is computed in tempSum, notice that the value is reported back to the caller. A sample program that uses the sum block is also shown in this video, which simply sums the values of a small set of integers and says the value to the screen.

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.