CPSC 351: Programming Languages
Lab 1 / Homework 2: Functional Languages (Scheme and Lisp)
Assigned: Friday 1/18/08
Due: Friday 2/1/08
In this lab, you will experience functional programming, using Scheme as the main language. We will be using Dr. Scheme ( on the lab computers. It is available for linux, windows, and mac systems at the above website (for free) if you want to install it on your own system.
When you start Dr Scheme, you will be given your choice of “languages”. The purpose of these is to restrict the full language and give error messages appropriate to what you are trying to do with the language. For our purposes, we can use the “Programming Languages” subset.
Part 1: Basic Scheme
1. Do Programming Exercise 1 on page 681. The formula for volume of a sphere is (4/3)*Pi*r3 .
2a. Write a function that changes a stoplight. The argument to the function is a color (red, green, or yellow). If the color is red, the function should return green. If the color is green, the function should return yellow, and if the color is yellow, the function should return red. (Use atoms for the value, and EQ? for the comparison). Use IF for the conditional.
2b. Repeat 2a using COND instead of IF
Part 2: Recursion
3. Type in the code of Problem Set #7 (p. 681) and experiment with it. Try it on simple and complex lists, such as ‘(a b c d e f) and ‘((a b) c (d e) f)). What does the function do? Explain (in English) how it works.
4. Write a recursive function to find the largest number in a simple list. Call your function my-max.
5. Programming Exercise 5.
6. Programming Exercise 10
Part 3: Functions as objects
7. Type in the definition of mapcar from Section 15.5.11.2 (Page 665).
a. Use mapcar and a lambda form to generate a list of the cubes of numbers 1 through 15
b. Use mapcar with a known function to generate a list of the first items of a list of lists. If the input list is ‘((a b) (a c) (a d) (a e f)) the result should be ‘(a a a a).
8.
a. Create a function called my-best that works like my-max, but takes 2 arguments. The first argument should be a function (name or lambda form). This function should return #T if the first argument is “larger than” the second argument
b. Show that calling my-best with > as the first argument generates the same results as my-max.
c. Using a lambda form, use my-best to find the list with the largest second element in a list of lists. (For example, when the list is ‘((1 2) (2 3) (4 1) (1 4)) the result should be (1 4)).
d. Using either a lambda form or a function that you write, use my-best to find the highest grade in a list of grades. Grades are atoms such as A+, A, A-, B+, B, B- ... D-, F