# Lab 6: Understanding Recursion
In lecture, we have started to write *recursive* functions, where a function body calls the same function to process a smaller part of the data. This lab gives you practice with understanding how this works and writing recursive functions of your own. In the first part of lab, you'll draw function diagrams on paper to visualize recursive function calls. In the second half, we'll practice writing some recursive programs. Call a TA over if you get confused or need clarification.
## Execution diagrams
You have seen an example of such a diagram in class and practiced reasoning about it in Drill 15. For a reminder,
:::spoiler **Execution diagram for `list-sum`**
![](https://hackmd.io/_uploads/SJ7ta9p-a.png)
Each of the boxes gives the *local context* for the corresponding function call (for example, the orange box gives the local context for the function call `list-sum([list: 3, 1])`, which is color-coded in orange). A *local context* is a part of the program directory created to keep track of the names specific to a function, while the function is running.
The vertical placement of the function calls is important, and shows how a function call is *pending* until the lower function calls finish. For example, `list-sum([list: 3, 1])` is pending until `list-sum([list: 1])` and `list-sum(empty)` finish. The arrows on the right side of the diagram show how the first answer we get back is for the empty case, and how having that answer lets Pyret resolve each pending function call in order.
:::
<br>
:::spoiler **Execution diagram for `my-pos-nums`**
![](https://hackmd.io/_uploads/HJNhn9a-p.png)
Notice that this diagram leaves off the local context for the call `my-pos-nums(empty)` and also leaves off the answers that each function call returns (both of which you should include in your diagrams). Also notice that the computation that `my-pos-nums` is doing at each step depends on the value of `f` -- this is because the [code that `my-pos-nums` executes](https://dcic-world.org/2023-02-21/processing-lists.html#%28part._pos-nums-eg-code%29) is different for different values of `f`, because of the `if`-expression.
:::
<br>
## Part 1: Creating Call-Record Chains
### Exercise 1: Binning Numbers by their Signs
In data analysis, we often take a column of data and convert it into a small number of fixed "bins": this lets us cluster data for analysis. For example, we refer to December, January, and February as "winter", while March, April, and May are referred to as "spring". The following function bins numbers based on their signs (positive, negative, and zero):
```
fun signs(numlist :: List<Number>) -> List<String>:
cases (List) numlist:
| empty => empty
| link(fst, rst) =>
sign =
if fst > 0: "pos"
else if fst == 0: "zero"
else: "neg"
end
# eval point A
link(sign, signs(rst))
end
where:
signs([list: 3, -1, 6, 0]) is [list: "pos", "neg", "pos", "zero"]
end
```
**Task 1:** On paper, draw an execution diagram for `signs([list: 3, -1, 6, 0])`. What function calls are pending at the moment that `signs(empty)` is called in your diagram?
**Task 2:** When people first see recursion, they often believe that the function will return the answer for the `empty` case (since in the code, the function returns a simple value in that case). What aspects of your diagram illustrate that this doesn't happen?
___
### *CHECKPOINT:* Call over a TA once you reach this point.
___
### Exercise 2: Creating Palindromes
A palindrome is a word whose characters are in the same order whether read from left to right or right to left. "abba" and "tacocat" are both palindromes.
The following code makes a list of letters into a palindrome:
```
fun make-palindrome(letters :: List<String>) -> String:
cases (List) letters:
| empty => ""
| link(fst, rst) =>
# here, + is being used to append strings
fst + make-palindrome(rst) + fst
end
where:
make-palindrome([list: "a", "b", "c"]) is "abccba"
end
```
**Task 3:** Draw the execution diagram for the call `make-palindrome([list: "a", "b", "c"])`.
___
### *CHECKPOINT:* Call over a TA once you reach this point.
___
### Exercise 3: Counting Emoticons
Impressed by your recursion skills, the engineers of a bee-whisperer chat service reached out to you asking for your help on some research. They want to study the frequency of emoticons used within their system. The importer has created a function `is-emoticon` that checks whether a text is a valid emoticon. The `count-emoticon` function uses `is-emoticon` to tally up the number of emoticons used in their service.
```
fun is-emoticon(msg :: String) -> Boolean:
(msg == ":)") or
(msg == "XD") or
(msg == ":'(") or
(msg == ":P")
end
fun count-emoticon(msgs :: List<String>) -> Number:
cases (List) msgs:
| empty => 0
| link(fst, rst) =>
if is-emoticon(fst):
1 + count-emoticon(rst)
else:
count-emoticon(rst)
end
end
end
```
Consider the following example:
```
count-emoticon([list: "XD", "LOL", ":P"])
```
**Task 4:** Draw the execution diagram for the call `count-emoticon([list: "XD", "LOL", ":P"])`
**Task 5:** Using a highlighter, different color pen, or label/symbol, indicate the calls that are pending *at the point when* `count-emoticon` is called with the empty list. Separately (using a different color or label), indicate the calls that are pending *at the point when* `is-emoticon` is called with the second element in the list, "LOL". *Hint:* Remember that the pending calls are the ones made on list elements that *precede* a given element (are higher up on the diagram), so you will have fewer pending calls indicated for this example than the previous one.
**Task 6:** Why don't calls to `is-emoticon` appear in your execution diagram? How is it used within `count-emoticon`? Write down your answer.
**Task 7:** Compare the first sequence of pending calls from task 5 to the sequence of pending calls for the `signs` problem. What do you notice is the same and different between the two sequences of calls? Write down your answer.
**Task 8:** For each of the three functions that you worked on, determine if the function's primary role is to `map` or to `filter` list elements. Identify the characteristics of these functions that differentiate between the two roles. Write down your answer.
<!-- **Task 9:** Now, think about whether each of the functions you worked on involved mapping or filtering. What hypothesis can you make about the sequence of "local context" terms for `map`-like functions versus `filter`-like functions? Write down your answer. -->
___
### *CHECKPOINT:* Call over a TA once you reach this point.
___
## Part 2: Recursive Programming Practice
**If you have been comfortable writing recursive programs in class, you can skip ahead to Part 3**
Imagine that you are playing a game that asks you to generate a collection of words from a specified set of letters. The following functions check various conditions against those words in order to score the game.
For each function that you implement, write sequential where-examples (as seen in class):
```
fun sum(numlist :: List<Number>) -> Number:
cases (List) numlist:
| empty => 0
| link(fst, rst) => fst + sum(rst)
end
where:
sum([list: 4, 7, 2]) is 4 + sum([list: 7, 2])
sum([list: 7, 2]) is 7 + sum([list: 2])
sum([list: 2]) is 2 + sum([list: ])
sum([list: ]) is 0
end
```
**Task 9:** Develop a function `all-have-t :: List<String> -> Boolean` that takes a list of words and determines whether every word in the list contains the letter `"t"`.
**Task 10:** Develop a function `compute-score1 :: List<String> -> Number` that takes a list of words and produces a score which is the sum of the lengths of all the words.
**Task 11:** Develop another scoring function `compute-score2 :: List<String> -> Number` in which the score for a single word is 1 if it has four or fewer characters. For words longer than four characters, its score is the length of the word.
**Task 12:** Look at the two functions you've just written. Is there a common pattern to writing "scoring"-style functions? At what point do they differ?
**Task 13:** If you aren't sure how these functions are working, draw out an execution diagram for each task to trace out how the computation is evolving.
___
### *CHECKPOINT:* Call over a TA once you reach this point.
___
## Part 3: Richer Recursion Practice
**Task 14:** Write a function `make-circles(rad-list :: List<Number>) -> Image` that takes in a list of numbers representing radii and produces an image consisting of adjacent green circles. Each circle should have the radius of its corresponding list index. In other words, the first circle should have the same radius as the first element of rad-list, and so on. The final circle in the image should always be a black circle of radius 10, which is not represented in the list.
For example, `make-circles([list: 40,60,10])` should produce an image that looks like the following:
![](https://i.imgur.com/IQ0T15T.png)
**Task 15:** Now, we want a function that lets us change the colors of the circles as well. The function `make-better-circles(rad-list :: List<Number>, color-list :: List<String>) -> Image` should take in:
- A list of Numbers representing radii
- A list of Strings representing valid Pyret colors
As before, the function should output an image of adjacent circles. This time, each circle should have both the color and radius of its corresponding list index. The first circle should be the same color as the first element of color-list and the size of the first element of rad-list, and so on. The final circle in the image should still be a black circle of radius 10, not in either list.
For example, `make-better-circles([list: 30,40,50], [list: "orange", "olive", "blue"])` should produce an image that looks like the following:
![](https://i.imgur.com/YhYb6bR.png)
**Task 16:** Draw out a sequence of related examples to show how the two lists interact within and across the examples. Since this involves images, you can draw the examples out on paper, but you should still be explicit about defining the input lists.
**Task 17:** Write the code for this problem. (*Note: Part of the point of this problem is to have you think through which of the two input lists is "driving" generation of the circles. That should be the list you break down in the `cases` expression.)*
:::info
Having trouble dealing with 2 lists? Apart from using `cases` statements, we can access a list's first and rest field by using `.first` and `.rest`. For example, if a list is named "my-list", we can access the first item in the list by typing `my-list.first`.
:::
**Task 18:** Modify your `make-better-circles` function to raise an error if there aren't enough colors in the color list for all of the radii
___
### *CHECKPOINT:* Call over a TA once you reach this point.
___
**More Challenging Task:** Write a new version of `make-better-circles` called `make-circles-recycle` in which the list of colors can be shorter than the list of radii. The circles should cycle through the list of colors in order, repeating until all circles have been generated. For example, given a list of four radii and the two colors `[list: "red", "brown"]`, the circles would appear in colors red, brown, red, brown.
*Warning: it is easy to make this unnecessarily complicated, especially if you have programmed before. This is an exercise in elegance. If you find yourself thinking about this by counting and modulo, for example, try again!*
___
### *CHECKPOINT:* Call over a TA once you reach this point.
------
> Brown University CSCI 0111 (Fall 2024)
> Feedback form: tell us about your lab experience today [here](https://docs.google.com/forms/d/e/1FAIpQLScnZizUwj7s0RsOt08CwTl1ZnE0wO8dGIpFxrG7EaUXbRq-pA/viewform)!