CS 341 - Survey of Programming Languages Homework #3

Due: Tuesday 9/20 at the beginning of class

Note: This work is to be done in pairs.  Each pair will submit one assignment.  Although you may divide the work, both team members should be able to present/describe their partner's work upon request.  All written exercises are to be done in a README file.  All Scheme code is to be done in a file hw3.scm.  All exercises should be clearly labeled or commented.

EOPL Exercises

• 1.12
• 1.13  Call your function subst2.
• 1.20
• 1.22
• 1.27
• 1.28
• 1.29
• 1.30
• 1.34

Test path for all numbers in the given test tree below.  This is easily accomplished using your definition of flatten and the expressions:

(define test-tree '(14 (7 () (12 () ()))
(26 (20 (17 () ())
())
(31 () ()))))
test-tree
(flatten test-tree)
(map (lambda (n) (path n test-tree)) (flatten test-tree))

Call the second sort sort-pred.   You may implement any recursive sorting method with worst-case time-complexity O(n2).  (Insertion sort can be implemented recursively.)  You may wish to do part 3 first and use that code for part 2 (a special case).

• Write a procedure free-vars that takes a list structure representing an expression in the lambda calculus syntax and returns a set (a list without duplicates) of all the variables that occur free in the expression.  Similarly, write a procedure bound-vars that returns a set of all the variables that occur bound in its argument.

Hint: Read in the Scheme reference section 6.3.2 about memq, memv, member, and append.  You may wish to create a "helper" or "auxiliary" function that enables you to keep track of the variables currently bound in any subexpression.

Perform at least these tests:
(free-vars '((lambda (x) x) y))
=> (y)
(bound-vars '((lambda (x) x) y))
=> (x)
(free-vars '((lambda (x) y) (lambda (a) (lambda (b) (a b)))))
=> (y)
(bound-vars '((lambda (x) y) (lambda (a) (lambda (b) (a b)))))
=> (a b)

• Give an example of a lambda calculus expression in which a variable occurs free, yet the expression has a value that is independent of the value of the free variable.
• Give an example of a lambda calculus expression in which the same variable occurs both bound and free.
• Extend the text definitions of occurs-free? and occurs-bound? to include Scheme if and let expressions.

For each exercise, you're required to include in your file the test examples given in the text.  In addition, you're encouraged to develop a good test suite beyond the limited text examples.