HtDP exercise: Managing microfinance borrowers

Credits: My WPI colleague Glynis Hamel designed a set of Kiva-inspired exercises for her intro course. Shriram Krishnamurthi prototyped the teachpack so we could pull in actual Kiva data. I put the two together and added some exercises for my students.

© Kathi Fisler, Glynis Hamel, Shriram Krishnamurthi: 2007


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.

  1. The information about a borrower consists of their name, size of team, 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.

  2. Write the template for functions over lists of borrowers.

  3. 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).

  4. 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.

  5. 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").

    [NOTE:We updated the teachpack in March 2008 to also extract data on the number of entrepreneurs involved in each Kiva group (this seems to be a new feature on their end in which groups of people request a collective loan). The former teachpack that assumed only single entrepreneurs (for backwards compatibility) is still available, at least for now.]

    The teachpack lets you use your code on actual data from the Kiva website. To use it, you first need a function to convert data from the website to your structure. The teachpack provides a constant sample-data that contains a snapshot of data taken from the website. This data is a list of lists of strings and numbers, one inner list per borrower. Using sample-data as a test case, write a function convert to convert the list of entries in the sample data to a list of your structures. The Scheme functions second, third, etc (up to ninth) can access items at those positions in a list (similar to first).

    Now you can hook up your code to the live Kiva data:

    Both of these functions will omit entries whose html on the website was formatted differently from our expectations.

    For more advanced students (uses intermediate language level).

  6. 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.

  7. 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.

  8. [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.