Lab 23: Iterating Again & Again & Again ...
Intro
You’ll work in this lab with your lab partners.
The two of you will work as a team to solve problems. At any time, one of you will be the Head and the other will be the Hands. The Head does the thinking and the Hands does the typing. Hands type only what the Head tells them to, but you’re free to discuss any issues that pop up. You should switch off during the lab to make sure each of you get practice problem solving, dealing with syntax, and getting finger exercises on the keyboard.
You should start this lab with this project skeleton.
Recall
We’ve seen many ways of iterating through data in this course. The fundamental list function foldr allows us to transform lists into any type of data, element by element using recursion. Iterators make it easy to check if a list is empty and mutably grab the next element, making them amenable to work with in for-each- and while-loops. The standard for-loop, which we’ve used little during the past semester, looks like this:
for (init ; test ; next) { |
body ... |
} |
and is roughly equivalent to the following while loop:
init; |
while (test) { |
body ... |
next |
} |
It can be used to print the first 100 natural numbers like so:
for (int i=0 ; i<100 ; i=i+1) { |
System.out.println(i); |
} |
These iterating operators are all slightly different, but should be able to be used to perform the same tasks. We’ll test that out today!
Summing a list of numbers
Summing numbers is one of the simplest tasks we’ve performed this semester, so it’ll be a nice warm-up for us.
Ex 1a: Use Listof.foldr to calculate the sum of a list of numbers.
Ex 1b: Use a for-each loop to calculate the sum of a list of numbers.
Ex 1c: Use a for loop to calculate the sum of a list of numbers. Hint: You may want to use an integer counter as an index via Listof.nth.
Ex 1d: Use a while loop to calculate the sum of a list of numbers.
Does such a string exist?
We also can check for the existence of a string satisfying some predicate inside a list of strings. The exists method already does this, but we can do this with iterating constructs as well!
We’ve provided two simple predicates in the test file. Use those to test for the existence of strings inside the list labc.
Ex 2a: Use Listof.foldr to test for the existence of strings satisfying the given predicates.
Ex 2b: Use a for-each loop to test for the existence of strings satisfying the given predicates.
Ex 2c: Use a for loop to test for the existence of strings satisfying the given predicates.
Ex 2d: Use a while loop to test for the existence of strings satisfying the given predicates.
Reversing a List
We can reverse a list with the method reverse, or we can use loops and temporary variables.
Ex 3a: Reverse the example list using a for-each loop.
Ex 3b: Reverse the example list using a for loop.
Ex 3c: Reverse the example list using a while loop.
Ex 3d: It’s more difficult (in a number of ways) to reverse a list using foldr than it is using the looping constructs. Why is that? Is there another list abstraction that would work better?
Average of a List
Calculating an average using loops is a bit tricker, since we need to update the average for each number as we go.
Ex 4a: Use Listof.foldr to calculate the average of a list of numbers.
Ex 4b: Use a for-each loop to calculate the moving average of a list of numbers.
Ex 4c: Use a for loop to calculate the moving average of a list of numbers.
Ex 4d: Use a while loop to calculate the moving average of a list of numbers.
Fizz Buzz
Fizz Buzz is a classic programming problem: the goal is to create a list of numbers from 1 to 100, where each number divisible by 3 is replaced with "fizz" and each number divisible by 5 is replaced with "buzz".
Ex 5a: Use Listof.foldr to create the fizz buzz list from 1 to 100.
Ex 5b: Use a for-each loop to create the fizz buzz list from 1 to 100.
Ex 5c: Use a for loop to create the fizz buzz list from 1 to 100.
Ex 5d: Use a while loop to create the fizz buzz list from 1 to 100.