Lab 5: Scheme Exercises (20 pts)

Due date: Tuesday, 25 November 2003

Update: Scheme available on csdev machines; 11/12/03; 8:31am

In this assignment, you will complete the following Scheme exercises to gain experience in programming in this variant of Lisp. The scm Scheme interpreter is available from ub.d.umn.edu. You can obtain a Windows or Macintosh Scheme interpreter from http://www.drscheme.org. Dr. Scheme is also available on the CS department csdev computers (csdev01 etc.). You will need an X-windows connection to these systems, and type "drscheme".

Exercises

1) [5 pts] Write a definition (using define and lambda) for the predicate function (a function that returns a boolean value, #t or #f) atom-occur? which takes two inputs, an atom (symbol) a and a list, s, and returns the boolean #t if and only if a appears somewhere within s, or in a sublist of s, recursively. Your function should use only recursion and not other forms of iteration. Examples of usage of the function are:

(atom-occur? 'a '((x y (p q (a b) r)) z)) ==> #t

(atom-occur? 'c '(x (y p (q a (b r)) z))) ==> #f

2) [5 pts] Write a definition for the function flatten, which recursively removes all parentheses except for the outermost pair from a nested sublist. Your function should use only recursion and not other forms of iteration. Examples of usage of the function are:

(flatten '((x y (p q (a b) r)) z)) ==> (x y p q a b r z)

(flatten '(x (y p (q a (b r)) z))) ==> (x y p q a b r z)

(flatten '(((a)))) ==> (a)

3) [5 pts] Write a definition for the function replace, which accepts three parameters: s, search-item, replace-item. The goal for the replace function to search the list s for all occurrences of item search-item, and replace those occurrences with replace-item. However, if replace-item is the empty list (), then all occurrences of search-item in s should be deleted. You need to look in the list s for search-item at all and any levels of nesting. A copy of the list s, with any changes, is returned as a result of calling the function. Your function should use only recursion and not other forms of iteration. Examples of usage of the function are:

(replace '(a b) 'a '()) ==> (b)

(replace '((x) a b c)) 'x '((m))) ==> ((((m))) a b c)

(replace '((x) a b c)) '(x) 'm) ==> (m a b c)

4) [5 pts] Write a function definition for the predicate bool-eval, which evaluates boolean expressions in fully-parenthesized prefix Scheme-like notation. The boolean operators your evaluator should accept are: and, or, and not. Your function should use only recursion and not other forms of iteration. Your function should not use the built-in Scheme expression evaluators (e.g., don't use 'eval'). For constants, you only need to be concerned about #t and #f. For logical symbols, have the bool-eval function accepts a second (possibly empty) binding set parameter, and interpret symbols in the boolean expression starting with '?' (e.g., '?x') as variables, which can be bound to #t or #f. Implement your binding sets as simulated Python Dictionaries, and test your function on boolean expressions including those containing instances of the variables. Simulate Python Dictionaries as lists (i.e., don't use the {} notation). The empty dictionary will be the empty list. Each binding in a binding set will be a pair within the "Dictionary" list. For example, ((?x #t) (?y #f)) will be a Dictionary with two variables, ?x and ?y, with ?x bound to #t and ?y bound to #f.

Your function should use only recursion and not other forms of iteration. Examples of usage of the function are:

(bool-eval '(not #t) '()) ==> #f

(bool-eval '(and #t #t) '()) ==> #t

(bool-eval '(and (or #t (not #t)) #f) '()) ==> #f

(bool-eval '(not ?x) '((?x #t))) ==> #f

(bool-eval '(and ?x ?y) '((?x #t) (?y #t)) ) ==> #t

Notes

There should be no reason to use global variables in your functions, but you can use local variables within your functions as needed (e.g., see let from class lecture). If you think you need global variables, please talk to the course TA or the instructor first.

If you find it useful, you can make use of auxillary (additional helper) functions in any of these functions.

Turn-in

Submit your properly documented program code in hard-copy, along with hard-copy of tests you have conducted with each function. Please also email your program code to your TA (Prashant Jain).