Assignment 6
Assignment 6
Lab Evaluations:
LAB DETAILS:
Lab Goals/Objectives:
• To learn using Lists in SNAP.
• Practice writing and reading functions with different input types.
• Determine how to break problems down into individual functions.
The grey rounded rectangle with red rounded rectangles inside it is the visual
representation of a list. Each red rectangle is one list item.
The list picture has several extra widgets: a + button, the down arrow, and so on. When
a list value is seen in a variable watcher, you can use these controls to modify the contents
of the list directly. But we're not doing that for a while, so for now just focus on the values
in the red rectangles.
Try these:
What value do you get if you call the list block with no inputs? (Use the left arrow to
delete the original input slot.)
Can you drag other reporters into list input slots to compute the list items?
What happens if you drag a list block into an input slot that expects a number as input,
such as the inputs to the arithmetic operators?
Create the variable Fruit and set the list name to Fruits.
Add 1 more fruit in the List.
Items Block
The Item block reports the value of the item at the specified
place on a list
Select from menu or insert a number to indicate which item you want.
Similarly, we can delete, insert, and replace any item from the list by using DELETE,
INSERT AND REPLACE WITH block respectively
You could build up a sentence out of phrases. For example, to make a noun phrase, you
want to pick one item from the articles list, one from the adjectives list, and one
from the nouns list.
To select one item from a list, use the item block:
List items are numbered from 1, so, for example, item 3 of the nouns list above is pizza.
The first input slot accepts a number like other rounded input slots, but it also has a
downward arrow that, when clicked, offers two special choices: last for the last item of
the list, and random to pick an item at random.
The second input slot in the item block is something you haven't seen before: a rectangle
with two orange smaller rectangles inside it. Just as a rounded input slot indicates that a
number is expected, and a hexagonal input slot indicates that a true or false value is
expected, this new kind of input slot means that a list value is expected. It's meant to look
like what you see when you say a list: a grey (rounded) rectangle with red-orange
rectangles for the individual items.
Use the item random feature to make a noun phrase by choosing a random article,
a random adjective, and a random noun:
Because of the random item choices, we get a different result each time we call noun
phrase:
Now suppose we'd like to be able to vary our noun phrases by sometimes using one of
these nouns in the plural. So we need a way to add the letter "s" to each item of the list.
Find the map block near the bottom of the Variables palette and use it this way:
The first input to the map block has a form you haven't seen before this:
The grey ring means that the input should be a function. What we mean by this is
basically the same thing as the f(x)=3x+7 kind of function in algebra, except that it doesn't
have to be a numeric function. In this case the function we want is "join the letter 's' after
the given word." When you drag the join block into the map input slot, the grey ring is
still visible, to remind you that the input is a function, not the word that you'd get from
some particular joining.
Instead of using a variable, like the x in f(x), to represent the input to the function, we
leave one of join's input slots as an empty box. This is supposed to remind you of the
notation 3×☐+7 ("three times box plus seven") that you learned for functions in
elementary school before you knew about variables.
Here are some more examples of using map to compute some function of every item of a
list:
In that third example, there are two empty boxes in the function, so it's ☐×☐, which
squares each number.
Like map, the keep block has a function as its first input. Notice, though, that this grey
ring's inner boundary is hexagonal. This lets you know that you should use
a predicate function, which means a function that reports true or false.
A function that, like map, keep items takes another function as an input is called
a higher order function
Now we want to select a subset of the states, namely the ones whose first and last letters
are the same. Here's the block we use for that:
We want to know whether two things are equal:
(Note that we've deleted the word "world" that Snap! provides as a default value, a hint
about what kind of input is expected. The input slot must be empty for our function-input
notation to work.)
Finding the last letter is a little trickier; you find the length of the text and use that number
to select which letter you want:
Putting all these pieces together will give us the answer we want:
You can see from the "length: 4" in the result that there are four such states.
But if you're playing Geography, the question you really want answered is "Which states
start with such-and-such a letter?" You can define a block that takes a letter as input and
gives the answer to that question:
Notice that the red length of block that finds the number of items in a list is different
from the green length of block that finds the number of letters in a text string.
The first input to the combine with block is a two-input function. In this case, it's
the + block, because we want to add all the numbers. Unlike the situation with
the map and keep blocks, there are really only a handful of functions you'll ever use
with combine:
Why did we include the addition and multiplication operators, but not subtraction or
division? Using an operator with combine makes sense only if it doesn't matter whether
the values are combined left to right or right to left. That is,
3+(4+5) = (3+4)+5
but
3-(4-5) ≠ (3-4)-5
Very occasionally you'll define a two-input custom reporter for use with combine. The
two-input max block is an example; try using that to find the largest of a list of numbers.
Introduction to Hyperblocks
Hyperblocks in Lieu of Map
As of July 2020, many blocks in Snap! have now been converted into "hyperblocks".
Roughly speaking, a hyperblock is a block which intuitively handles both list and scalar
values. A scalar value is simply a value which is not a list.
Hyperblocks allow us to write shorter (and arguably easier to understand) code in some
places where we might have used the map function. For example, the +, x, and letter of
blocks are all now hyperblocks, allowing us to write some familiar examples without map:
Hyperblocks in Lieu of Combine
Hyperblocks can also replace the combine function. For example, consider the code
below, which combines the given Strings using the combine block.
Since join is a hyperblock, you can also just provide it a list, and it'll join all of the elements.
Just like with map and combine, it takes a lot of practice to realize what exactly
hyperblocks are capable of. Hyperblocks cannot fully replace map and combine. For
example, suppose you wanted to sum a list of numbers. Since the plus block always
returns a list of numbers when you provide it with a list (i.e. its range is "list" when one of
its inputs is "list"), there's no way to use the plus block to sum a list. Instead, you'll have
to use combine as shown below:
Now suppose instead that we want to select the odd numbers from a list of numbers. It's
a little tricky figuring out how to tell if a number is odd, but apart from that it's a
straightforward keep problem:
But what if we want the squares of the odd numbers? This is neither a simple map nor a
simple keep, but combines aspects of both. And we can solve the problem by using the
value reported by the keep as the list input to map:
Don't be confused about which inputs do and don't have rings. It's the
square function and the are-you-odd? function that we use as the first input to each
higher order function. But, even though keep itself is a function, it's the list reported
by keep that we're using as the second input to map.
Or if we wrote this code using the hyperblock feature of the times operator, it might look
like:
In this section, we'll combine everything we know to make a better version of our acronym
block. For example, if we provide the input "University of California Berkeley", the block
will report the text UCB. Or if we provide the input "Beauty and Joy of Computing," the
block will report the text BC. Here, our acronym block will keep the first letter of any word
that is 5 or more letters.
In addition to only keeping words of 5 or more letters, another big difference between this
block and your previous acroynm block is that this block accepts a text. That is, instead
of the input being a list of four text values ["University", "of", "California", "Berkeley"], the
input will just be one text value that says "University of California Berkeley".
To translate from a text string to a list of words, you will need to import the
sentence->list block by importing the Words and Sentences library. To import the
library, click on the file icon in the upper left hand corner of Snap!, select Libraries, and
import the Words and Sentences library.
Try this: Define an acronym block that behaves as described above and as pictured
below.
You may use any combination of higher order functions and hyperblocks that you'd like.
If you're stuck, look at a pure HOF solution with no hyperblocks
Task # 2:
In the last activity we made a list of Beatles:
Use map and this list to get a list of just the first names of the Beatles.
Task # 3:
(a) Write an expression that will select all the words of at least five letters from a list. For
example, if the words in the list are being, for, the, benefit, of, mister,
and kite, then your block should choose the words being, benefit, and mister.
(b) Write an expression that takes a list of mixed words and numbers, and selects just
the numbers, (Hint: Look for is in the Operators palette.)
Task # 4:
Suppose we have a list of strings like this:
Create an acronym-of-list block that returns the first letter of each string, all joined
together. As an example, see the image below. You should not use map or combine when
creating your block!
You'll notice that the resulting acronym is a little ugly, i.e. "UCB" would probably be a
better choice than "UoCB".
Modify the acronym block so that, instead of keeping long words, it keeps words that
start with a capital letter. (Hint: Experiment with the unicode of block.) Try the
following examples:
Task # 5:
Make a block that takes a sentence from the user and give us the largest word in the
sentence.
Task # 6:
Make a block that swap two items in the list.
Task # 7:
Write the script that takes a fruit name from the user and checks whether this given fruit
is in the list or not. If the fruit is in the list, then delete it from the list.
Task # 8:
Write the Script for the Library management system with the following functionalities
i) Add Book.
Submission Instructions: