On this page:
1 Introduction
2 Assignment
2.1 Recommend a Book
2.2 Recommend a Pair of Books
2.3 Example:
2.4 Built-Ins
3 Handing In
4 Template Files

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.

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 Assignment

You need to organize the list of recommended books to support two tasks:

2.1 Recommend a Book

When someone buys a book, you want to be able to suggest a second book to accompany it. Specifically, you should provide the book it is most frequently paired with in the recommendation lists, 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, as described below.

2.2 Recommend a Pair of Books

Sometimes, an indecisive user asks for a book recommendation. 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).

A list of recommended books is represented as a File:

data File:

    | file(name :: String, content :: String)

end

Each file contains a single input list, with single book descriptions on each line. That is, book descriptions will be separated by "\n". Each book has a unique and unambiguous description; that is, if two lines in two different input lists are identical, they refer to the same book. Otherwise they refer to different books. Input lists will always contain at least two books, and they will never contain duplicates. For example, an input file could be

file("vty.txt", "Crime and Punishment\nHeaps are Lame\nLord of the Flies")

An output recommendation is represented as a Recommendation:

data Recommendation:

    | recommendation(count :: Number, names :: List<String>)

end

2.3 Example:

f1=file("alist.txt","1984\nAnimal Farm\nHigurashi\nLife of Pi")

f2=file("blist.txt","Animal Farm\nHigurashi\nLife of Pi")

f3=file("clist.txt","1984\nHeart of Darkness")

 

check:

  recommend("1925",[list: f1,f2,f3]) is

    recommendation(0,[list: ])

  recommend("1984",[list: f1,f2,f3]) is

    recommendation(1,[list: "Animal Farm","Higurashi","Life of Pi","Heart of Darkness"])

 

  popular-pairs([list: f1,f2,f3]) is

    recommendation(2,[list: "Animal Farm+Higurashi","Animal Farm+Life of Pi","Higurashi+Life of Pi"])

end

fun recommend(title :: String, book-records :: List<File>) -> Recommendation

recommend takes a book title and a list of Files and produces a Recommendation. The Recommendation’s names 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 names list are each paired with the input book.

fun popular-pairs(records :: List<File>) -> Recommendation

popular-pairs takes a list of Files and produces a Recommendation. The Recommendation’s names is a list of Strings each of which represent a pair of books. Each of these Strings contains two titles separated by a "+", for example "book1+book2". Each most popular pair of books should appear exactly once and order is irrelevant. The Recommendation’s "count" is the number of times each pair occurred together in a file.

2.4 Built-Ins

For this assignment, you will need to write your own version of any built-in functions that you choose to use.This is to force you to practice: you should be able to write such functions without much difficulty, but studies show that drill is a really good way to get better at programming.

For strings, you may only use string-to-code-point, string-from-code-point, string-to-code-points, and string-from-code-points. Built-in string functions can be recreated by operating on lists of code points. You are, however, welcome to use operators on Strings (+, <=, >, ==, etc.).

For lists, you may use list.first and list.rest. 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).

3 Handing In

Use Captain Teach.

4 Template Files

Implementation Code