This lab is designed to help you practice programming over lists of structures.
You've now seen three operators for building lists: cons, list, and append. Their contracts are as follows:
For each of the following expressions, try to predict (a) what they will return and (b) what the length of the resulting list is, then check your answers in DrScheme:
(cons empty empty)
(list empty empty)
(append empty empty)
(cons (list 1 2 3) (list 4 5))
(list (list 1 2 3) (list 4 5))
(append (list 1 2 3) (list 4 5))
Kiva.org connects individuals with microfinance opportunities in the developing world. Small business owners in developing countries request loans through Kiva. Kiva advertises these requests and lets individuals contribute small amounts to borrowers of their choice.
In these exercises, you will develop data definitions for key Kiva information and functions to process that information. You will also hook up your code to the Kiva website, so you can test your code on Kiva's actual data. This is a big part of what you'd need to do to write custom scripts or mashups based on Kiva's data.
You are welcome to use filter for these exercises.
The information about a borrower consists of their name, home country, type of business, requested amount, and percentage of the requested amount raised so far. Develop a data definition for a list of borrowers and an example using your data definition.
Write the template for functions over lists of borrowers.
Write a function funds-needed
that consumes a list
of borrowers and produces the total amount of money that these
borrowers are still seeking (ie, the sum of the amounts requested but
not yet raised across all borrowers).
Write a function find-by-country
that consumes a
country and a list of borrowers and returns the list of borrowers who
are from that country.
Download and install this kiva-teachpack. (Install it by going to the "Language" menu, selecting "Add teachpack", then "Install teachpack" on the dialog box. Once the teachpack name appears in the right-hand box, highlight it and click "OK").
The teachpack provides a function get-kiva-data
that
takes no arguments but returns a list of lists of strings and numbers,
with each inner list containing the data for one borrower. Write a
function to convert the list of kiva data to one of your list of
borrowers, then try your functions on the real dat. The Scheme
functions second
, third
, etc (up to
ninth
) can access items at those positions in a list
(similar to first
).
If you are using version 371 (on your laptop or at home), you can
access multiple pages of kiva data using the teachpack function
get-kiva-data/page
which takes a page number (currently 1
through 5) and extracts the data on the given page number on the kiva
site. This function does not work in version 370.
Everyone should be able to finish up to this point.
If you get here and want more of a challenge, skip to the last problem.
Write a function search-borrowers
that consumes a
list of borrowers and a function from borrower to boolean (ie, a
predicate on borrowers) and returns a list of all borrowers for
which the function returns true. Use your function to reproduce the
results of find-by-country
.
Use functions you've already written to write
funds-by-criterion
that consumes a list of borrowers and
a predicate on borrowers and produces the total amount that borrowers
who satsify the predicate are still seeking.
[More Challenging] Write a function sort-by-need
that consumes a list of borrowers and produces a summary of how much
funding is needed per country (over all countries represented in the
list), with the summary results sorted by decreasing need. Entries in
your summary should indicate both the country and the amount required.
Compose existing functions where you can to write this, without
worrying about the efficiency of your solution. You may use Scheme's
built-in quicksort
function; use the helpdesk to find its
contract.