Nile
1 Introduction
You are creating Nile.com, which you hope will be the next big thing in online bookstores. You know that you can save money by anticipating what books people will buy; you will pass these savings on to your users by offering a discount if they buy books that Nile recommends.This assignment is meant to introduce you to the concept of collaborative filtering.
To do this, you offer an incentive for people to upload their lists of recommended books. From their lists, you can establish suggested pairs. A pair of books is a suggested pair if both books appear on one person’s recommendation list. Of course, some suggested pairs are more popular than others. Also, any given book is paired with some books much more frequently than with others.
2 Theme Song
Walk Like an Egyptian by the Bangles
3 Assignment Starting Points
First, work through the Druid. (If it looks like it’s taking too long to load, reload the page.) To understand the Druid, watch this brief video.
Then start working with the template.
4 Warning
Past students have found this assignment quite difficult relative to when it appears in the course. Think hard about decomposing the problem into simpler pieces rather than trying to solve it entirely in one step. Give yourself enough time!
5 Assignment
A list of books recommended by one user is represented as a File:
data File:
| file(name :: String, content :: List<String>)
end
file("vty.txt", [list: "Crime and Punishment", "Heaps are Lame"])
You need to use Files of recommended books in the two following tasks.
A book title cannot be the empty string.
5.1 Recommend a Book
When someone buys a book, you want to suggest other books to accompany it. Specifically, you should provide the book that is paired most frequently with the book the user bought, along with a count of how frequent this is. Because there may be more than one book with that count, you should return a list of books (even if there is only one) as well as the count. This will be in the form of a Recommendation:
data Recommendation<A>:
| recommendation(count :: Number, content :: List<A>)
end
Define the following function:
fun recommend<T>(title :: String, book-records :: List<File>) -> Recommendation<String>
recommend takes a book title and a list of Files and produces a Recommendation. The Recommendation’s content is a list of the titles of the books that are most often paired with the input book. The Recommendation’s count is the number of times the books in the content list are each paired with the input book. When no pairing can be found, recommend should return recommendation(0,[list: ]).
5.2 Recommend a Pair of Books
Sometimes, an indecisive user just wants a recommendation of good books to read, not based on their previous purchases. Nile offers not one recommendation at a time, but pairs of them! Wow! To support this, you must be able to identify the most popular pairs of suggested books. Return the most popular pair, with a count of how often it occurs. Again, because there may be multiple pairs with the same count, you should return a list of pairs (even if there is only one).
Define the following function:
fun popular-pairs(records :: List<File>) -> Recommendation<BookPair>
popular-pairs takes a list of Files and produces a Recommendation. The Recommendation’s content is a list of BookPairs (defined below). Two books form a pair if they appear together in a given input File. The output of popular-pairs is the most frequently occurring pair (or pairs) found in all the input Files. So, if there is a tie for the most popular pair, all tied pairs are returned in the list. Each pair should appear exactly once and order is irrelevant. The Recommendation’s count is the number of times each pair occurred together in a file. When no pairing can be found, popular-pairs should return recommendation(0, [list: ]).
data BookPair:
| pair(book1 :: String, book2 :: string)
end
5.3 Built-Ins
For this assignment, you will need to write your own version of any built-in functions that you choose to use, other than the higher-order functions map, filter, and fold.This is to force you to practice: you should be getting able to write such functions without much difficulty. Also, studies show that drill is a really good way to get better at programming.
You are welcome to use operators on Strings (+, <=, >, ==, etc.).
For lists, you may use list.first, list.rest, map, filter, and fold. You may not use any built-in functions such as length, member, get, split-at, etc. You should be able to write your own versions of these functions using link and empty.
When writing your own version of built-in functions, we do expect to see all the steps of design recipe, including a reasonable sample of examples (but not a detailed test suite).
6 Comments
Because we have not yet discussed any efficiency measures in class, you are not asked to consider the efficiency of your implementation.
7 Handing In
You will submit three files, nile-code.arr, nile-tests.arr, and nile-common.arr.