Collaboration Policy: You may collaborate as much as you want on this assignment (this is more flexible than the normal course policy). The goal is to get everyone up to speed on functional programming. You are required to turn this in, but it will be weighted lightly in final grades. That said, we strongly encourage you to actively try writing these on your own while collaborating, as assignments after we come back together will assume you can do these sorts of problems on your own.
Need help? Find us, as well as questions and answers from other students, on Ed. Here's our office hours schedule.
Handin Instructions are at the end of the document.
filter
and map
, two functions built into many programming languages for performing common operations on listshw1a-15-code.arr
.Follow these style guidelines when writing your code:
When defining functions make sure to name them exactly what is given in the handout. If you do not do this, the autograder will fail.
The handout guides you through developing the code in stages. You will turn in one set of files with your cumulative work on all tasks. You do not need to maintain versions of your code from each task separately.
If you'd like help contextualizing Pyret through the lens of Java, check out our Pyret crashcourse for those with Java experience.
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. The list documentation may be particularly useful for this assignment.
Note: Click on blue headers throughout for a fun surprise :)
It's the early 2000s! There's no social media. Cell phones are just becoming popular. People rely on email to communicate electronically. Let's use email as a sample domain to practice manipulating lists of data in a lightweight way.
…we've been swapping a lot of emails. Start with the following data definition for email messages:
Check out the Pyret textbook for more info on data definitions.
In OOP speak, we can think of message
as a constructor. The way we actually make one of these data is by calling message
with three parameters; for instance:
In order to access a parameter's value, you can use .
. For example, in order to access the content of Joe's message, you would write msg.content
.
Task: Make a list of email message objects to use as test data. Stick to short contents, but have a mix of read and unread messages from a small number of email addresses. Also include multiple emails from the same sender (needed for later).
Task: We want to see which emails we haven't read yet. Use filter
to write an expression that gets all of the unread messages from your test data.
Task: Now we want to mark all emails as 'unread'. Use map
to write an expression that produces a new copy of the test data in which all of the messages are now unread.
Note: Expressions are NOT the same as functions. x + y
and 1/5
are examples of expressions.
Task: Write a function unread-messages-from
that takes a list of email messages and a sender's email and returns a list of unread messages from that sender.
Task: Write a function messages-about
that takes a list of email messages and a word or phrase (string) and returns a list of all messages that contain that phrase.
Task: Write a function unread-from
that takes a list of email messages and returns a list of unique usernames (without the "@..."
suffix) for whom there are unread messages. Use the built-in method distinct
that takes a list and returns a list of the unique elements to trim duplicates.
A weather station needs to compute average rainfall over a period of time. Their data are available as a list of numbers. Negative numbers in the data are ignored as noise (ie. sensor malfunctions). One negative number, -999, is special: it may be used to indicate the end of the time period of interest (so all values that follow -999 are to be ignored, whether positive or negative). There is always at least one non-negative number before either -999 or the end of the list.
Here's an example of the function that they need:
rainfall([list: 1, -2, 5, -999, 8])
is 3
(the average of 1 and 5)
Task: Write the rainfall
function as described above.
Task: Write a check
block with a thorough set of test cases for rainfall
. Thorough tests catch reasonable logic errors or functionality omissions that someone might make in writing this program.
Useful built-ins:
take-while
, like filter
, takes in a predicate (a function that maps Item -> Boolean
) and a list as arguments. It splits the list into a tuple denoted by {<sub-list-1>; <sub-list-2>}
. The function takes elements in the list until it encounters the first element where the predicate is false. The 'taken' elements are returned in a list as <sub-list-1>
, and the rest of the elements are returned in a list as <sub-list-2>
. For example:.{<index>}
..get(<index>)
.sum
takes a list of numbers and adds them up. To use sum
, you must have include math
at the top of your file.length
takes a list and returns its length.An online clothing store applies discounts during checkout. A shopping cart is a list of the items being purchased. Each item has a name (a string like “shoes”) and a price (a real number like 12.50).
Each item is represented as follows:
Task: Write a function called checkout
that takes a list of Item
(this list represents the shopping cart) and produces the total cost of the cart, reduced (where applicable) by applying the following two discounts:
Example:
checkout([list: item("shoes", 25), item("bag", 50), item("shoes", 85), item("hat",15)])
is (25 + 50 + 85 + 15) - ((25 + 85) * 0.2)
Task: Write a check
block with a good set of tests for checkout
. Annotate each with a description of what situation the test is designed to check.
For this homework, you MUST submit your solutions in a file called:
hw1a-15-code.arr
For autograder compatibility, please check that all required function names exactly match the functions named in this handout, and that the file is named correctly. Our autograder will break and you will lose points if your function names are not correct.
You may submit as many times as you want before the deadline. Only your latest submission will be graded (though you will spend late days if the latest submission is after the due date.)
To hand in your solution, you must upload them to Gradescope. Do not zip or compress them. Read our Gradescope Submission Guide for help on submitting to Gradescope.
Please let us know if you find any mistakes, inconsistencies, or confusing language in this or any other CS200 document by filling out the anonymous feedback from!