---
tags: labs, spr22
---
# Lab 1 (CS0150): Working with Lists in Pyret
Welcome to your first CS0200 lab! This lab will get you familiar working with Pyret datatypes, and manipulating lists using higher-order procedures (HOPs) and/or recursion (which will be very helpful for Homework 1!).
## Set-up (let's [get the party started](https://www.youtube.com/watch?v=mW1dbiD_zDk))
* Go to [code.pyret.org](https://code.pyret.org/); this requires logging in with any Google account (like your Brown account).
* Make a new file in code.pyret.org.
* Copy and paste this [stencil code](https://code.pyret.org/editor#share=1-b26xlHS3ANxeehePmtyISE3YYl3Dp8l&v=1904b2c) into your Pyret file.
### Documentation
Once you have opened the editor, the Pyret documentation is accessible from the Pyret logo button in the top left corner of code.pyret.org.
For this assignment, you will find the [list documentation](https://www.pyret.org/docs/latest/lists.html) useful. We recommend briefly browsing this page before working on these problems!
### Staff Assistance
Your friendly lab TAs are here to help! Sign up on your lab's queue if you need help at any point in this lab.
### Pair Programming
In CS0200's labs, you are expected to pair-program with different partners in each lab. Pair programming means that you're always working on the same piece of code, and one of you is typing while the other is telling them what to do and watching for mistakes, switching off coding and following in roughly 15 minute intervals (you can choose your stopping points). Mostly, just make sure everyone gets to code and also everyone gets to coach pretty equally. You should read our [Pair Programming Guide](https://hackmd.io/388rC8byRXq0bTRsFY1-WQ)!
## [Chasing Pyret](https://www.youtube.com/watch?v=uTxythHY09k)
**Task**: Translate the following interface/inheritance and containment/association diagrams into Pyret data structures. Write the data definition(s) into your code file. [Here's](https://docs.google.com/document/d/1LZV1M75gozJuU7jouTmusjw2B4fjLPOCSh9feQ3sDXM/edit#heading=h.n64vzvb7650b) the i/i and c/a diagram guide for reference.

**Task**: Create two empty lists, `all-students` (which stores instances of `Student`) and`all-courses`(which stores instances of `Activity`). We're going to use these empty lists for the next set of tasks.
## [Shop 'til you drop](https://www.youtube.com/watch?v=QK8mJJJvaes)
It's shopping period and everyone wants to know who's in their class!
**Task**: Use your Pyret datatype from above to create the following 6 `course` objects. Add all the courses into the `all-courses` list.
| # | dept | num | prof | credits
| -------- | -------- | -------- | -------- | -------- |
| 1 | VISA | 2320 | Alex Ryan | 1
| 2 | COLT | 10 | Ricky Chau | 0.5
| 3 | ENGL | 9000 | Brendan Ho | 1
| 4 | CSCI | 200 | Kathi Fisler | 1
| 5 | VISA | 100 | Daniel Stupar | 1
| 6 | ENGL | 100 | Brendan Ho | 1
**Task**: Use your Pyret datatype from above to create the following 3 `indepStudy` objects to represent independent studies (ISPs):
| # | advisor | credits
| -------- | -------- | -------- |
| 1 | Austin Miles | 1
| 2 | Daniel Segel | 2
| 3 | Truman Cunningham | 2
**Task**: You want to make a schedule for two of your best friends + Alyssa! Luckily you already made course datatypes. Use your Pyret datatype from above to create these three students with the following `Activity` objects. Add all the students into the `all-students` list.
| # | name | taking |
| -------- | -------- | -------- |
| 1 | Raphael | VISA2320, VISA100, Daniel's ISP, Austin's ISP |
| 2 | Alyssa | COLT10, CSCI200, VISA2320 |
| 3 | Cindy | VISA100, ENGL9000, ENGL100, Truman's ISP |
You and your friends are competing to get into VISA0100. You want to figure out how to take in a list and return only the students taking VISA0100.
**Checkpoint 1:** Call over a TA and make sure you've instantiated all of the data correctly!
**Task:** Write a function `get-visa-students` that takes in a `Student` list and returns a list of all students taking VISA0100.
::: spoiler *Hint:* How do I handle variants with different fields within one datatype?
It might seem tricky to work with the `Activity` datatype because it has two variants which each have different fields. If your function needs to check a field that isn't present in all variants, you can use case-matching. For example:
```
data Animal:
| goldfish(name :: String, age :: Number)
| puppy(weight :: Number)
end
fun get-name(pet :: Animal) -> String:
cases (Animal) pet:
| goldfish(n, _) => n
| puppy(_) => "Give the puppy a name!"
end
end
```
The `_` means it's a field we're not going to use. You can read the [full Pyret documentation here](https://www.pyret.org/docs/latest/Pyret_Style_Guide.html#%28part._.Cases%29).
:::
\
**Task:** Oops! The C@B searchbar crashed again. Write a function `find-course` that takes in an `Activity` list, a department code string, a course number, and returns a list containing the matching `course` object.
**Task:** You hear the professors doing independent studies have easier classes. Write a function `get-isp-profs` that takes in an `Activity` list and returns a list of strings of all the professors supervising ISPs this semester.
**Task:** Hmm...some of these courses should be worth more credits. Write a function `increase-credits` that takes in an `Activity` list and returns a new list in which all courses numbered 2000 and above are now worth 1.5 credits.
**Task:** Brendan's going on sabbatical! Write a function `replace-prof` that takes in an `Activity` list and returns a new copy of the list of all courses, except that all courses taught by "Brendan Ho" are now taught by "Qinan Yu".
**Checkpoint 2:** Call over a TA and check if your functions are correct!
## Recursion Practice
In Monday's lecture, Milda introduced the practice of writing a sequence of related examples in a `check` block. This can be an extremely useful practice for figuring out how to write a program that builds up an answer while traversing a data structure (whether by iteration or recursion). Let's practice that skill.
:::spoiler Screenshot from class if you need a reminder
This was for a function that doubles all values in a list

:::
**NOTE:** For the next three questions, you are only writing `check` blocks, you are **NOT** writing the functions.
**Task:** Develop a sequence of related examples in a `check` block for a function `concat-all` that takes a list of strings and returns the string resulting from concatenating (gluing) all of those strings together (e.g., `concat-all[list: "a", "b"]` produces `"ab"`). In Pyret, `+` concatenates strings.
**Task:** Develop a sequence of related examples in a `check` block for a function `below-5` that takes a list of numbers and returns a list of those numbers that are smaller than 5.
**Checkpoint 3:** Call over a TA and check whether your sequences have been done properly
Work on the following tasks as time permits in lab (we know some students won't get this far)
**Task:** Implement `concat-all` (for which you developed examples)
**Task:** Implement `below-5` (for which you developed examples)
**Task:** Develop a sequence of related examples in a `check` block for a function `all-profs` that takes a list of `Activity` and returns the list of names of professors who are teaching those activities (duplicates are fine). If you have time, write the code using recursion (rather than `map` -- the goal here is to practice recursion on small examples before we get to larger ones next week).
<!--
## TAs Work on things before this point -- Kathi will finish rest later
Kathi still has to deveop these
- Tracing recursion
- Recursion Practice
- total credits that a student is taking this semester
- average credits taken by all students
-students taking fewer than 2 courses
-->
<!--
Kathi may not put these in
#| Program Planning: Which of map, filter, neither can you use to solve these. Discuss with partner
- sorting courses by dept and number
- finding profs teaching more than 3 credits
- returning courses that students are taking concurrently with CSCI 200
- finding professors teaching in more than one department
|#
-->
______________________________
*Please let us know if you find any mistakes, inconsistencies, or confusing language in this or any other CSCI0200 document by filling out the [anonymous feedback form](https://docs.google.com/forms/d/e/1FAIpQLSdFzM6mpDD_1tj-vS0SMYAohPAtBZ-oZZH0-TbMKv-_Bw5HeA/viewform?usp=sf_link).*