CSC 153 Grinnell College Spring, 2005 Computer Science Fundamentals Laboratory Exercise

# Conditional Execution in Scheme

## Goals:

This laboratory exercise helps you gain some experience developing and using Boolean expressions.

## Steps for this Lab:

1. Evaluate the following expressions:
```
(and #t #t)
(and #f #t)
(and #t #f)
(and #f #f)
(and #t #t #t #t)
(and #t #t #f #t)
(or #t #t)
(or #f #t)
(or #t #f)
(or #f #f)
(or #t #t #t #t)
(or #f #t #f #f)
(or #f #f #f #f)
(not #t)
(not #f)
```
In each case, explain why Scheme produced the result given.

2. Now, evaluate the following expressions:
```
(and 'cat #t)
(and #f 'cat)
(and 'cat #f)
(and #f #f)
(and #t #t 'cat #t)
(and #t #t #t 'cat)
(and #t #t #t 'cat 'dog)
(or #t 'cat)
(or 'cat #t)
(or #f 'cat)
(or 'cat #f)
(or 'cat 'dog)
(or #f #f)
(not 'cat)
```
In each case, state why you think Scheme produces the result given. Generalize the results to indicate what values and, or, and not return when they are applied to non-Boolean values.

3. Consider the following Scheme procedure:
```
(define not-and
(lambda (A B)
(not (and A B))))
```
Explain why this procedure evaluates the logical expression "Not (A And B))".
Apply this procedure to various values #t, #f) for A and B. In each case, be sure you understand why the machine prints the output that results.

4. Write procedures to evaluate "(A And B) Or C" and "A And (B Or C)".
Run these procedures with various values (#t, #f) for A, B, and C, and examine the output. In each case, be sure you can explain the resulting output.

5. Enter into Scheme the procedure type-of-number, as defined in the reading for this lab, and apply it to several numbers. In each case, explain how Scheme obtained its answer.

6. Change the previous procedure to compare2 which is applied to two numbers A and B. If A < B , the procedure should return "The first number is smaller than the second.". If B is smaller, the procedure should return "The first number is greater than the second." If both numbers have the same value, the procedure should return "The numbers are equal."

7. Program ~walker/153/labs/smallest.ss contains the shell of a program that is supposed to identify the smallest of three numbers.

• Copy ~walker/153/labs/smallest.ss to your account.

This may be done with the copy or cp command as follows:

```
cp  ~walker/153/labs/smallest.ss  .
```
This command should be executed in a terminal window. Within this command, the final dot . is an abbreviation for your current directory. Thus, in this expression, you are telling the machine to copy (cp) the current file ~walker/153/labs/smallest.ss to your current directory. The new file will appear in your directory with the same name, smallest.ss. If you wanted the program to have a different name (e.g., my-program.ss), then you could change your copy command to:
```
cp  ~walker/153/labs/smallest.ss  my-program.ss
```
• Fill in the Boolean conditions (indicated as ??? in the file) to test for the cases specified.

• Test your procedure with several examples which cover each case.

8. Rewrite your procedure smallest, replacing the cond by if expressions. Thus, the main part of your procedure may have the form:
```
(if (???1)
"A is smaller than both B or C." #then clause
(;;; the else clause -- A is not smaller
if (???2) #second condition within cond
"B is smaller than both A and C."
;;; the second else cause -- B is not smaller either
??? etc.
)
)
```
9. Challenge Problem 1: Program ~walker/153/labs/triangle.ss contains the shell of a program that is supposed to determine if a triangle can be made using segments of three specified sides. If such a triangle can be formed, then the program also is supposed to identify the type of triangle.
Copy this program to your account, and fill in the Boolean conditions.

10. Challenge Problem 2: Rewrite your procedure triangle, using nested if expressions instead of cond.

#### Work to be turned in:

• Results of steps 1 and 2, together with explanations of the results obtained.
• Explanations for steps 3, 4, and 5.
• Code for steps 6, 7, and 8. In each case, include the results from some test cases.

Note: Challenge Problems 1 and 2 are not required, but you may do either (or both) for extra credit.

This document is available on the World Wide Web as

```http://www.cs.grinnell.edu/~walker/courses/153.sp05/labs/lab-cond-if.shtml
```