# Class 09: Pause for Breath

Back to Conditionals Lab. On to Characters and Strings.

Held Friday, September 13, 2002

Summary

Today we pause for a day to consider issues in the design of Scheme procedures and to help resolve any confusions you may have over past labs.

Assignments

Notes:

• Don't forget to save your daily work in a file.
• Warning! Friday the 13th falls on a Friday this month.

Overview

• Homework 1
• Problems?
• swap-first-two
• between
• Others?
• Program design
• Indentation
• Comments (at least two types!)

## Homework 1

• We'll spend a few minutes looking at the sites you built for homework 1.
• Since your sites are nice, I'll probably highlight negatives rather than positives. (Sorry, that's just how I am.)
• By revealing things you did less well, I hope to encourage you to do better things next time.

## Some Recent Problems

### Swap First Two

• To get the first element of the list: `(car lst)`
• To get the second element of the list: `(car (cdr lst))`
• To get all but the first two elements: `(cdr (cdr lst))`
• To shove the first element on the front of that reduced list:
`(cons first-element reduced-list)`
• To shove the second element on the front of that list:
`(cons second-element previous-list)`
• Putting it all together
```(define swap-first-two
(lambda (lst)
(cons (car (cdr lst))
(cons (car lst)
(cdr (cdr lst))))))
```

### Between

• Reminder, `(between a b c)` should return true (#t) if b is strictly between a and c and false otherwise.
• Here's one solution I saw
```(define between
(lambda (a b c)
(cond
((and (< a b) (< b c)) #t)
((and (> a b) (> b c)) #t)
(else #f))))
```
• While this is correct, there are a few ways to make it more elegant.
• Fix one: Merge comparisons. If you read the instructions carefully, you'll see that the < (less-than) and > (greater-than) procedures can take more than two parameters, and determine if their parameters are strictly increasing or decreasing. Instead of the `ands`, just use three parameters.
```(define between
(lambda (a b c)
(cond
((< a b c) #t)
((> a b c) #t)
(else #f))))
```
• Fix Two: Use results directly. The current code has something like If an expression is true, use true. Since the value returned is the same as the value of the expression, why not just use that?
• b is between a and c if (a b c) is strictly increasing or (a b c) is strictly decreasing.
• In Scheme
```(define between
(lambda (a b c)
(or (< a b c) (> a b c))))
```

### Inexact Integers

• A quick question based on past material. Without using `exact->inexact`, can you define a value, v, such that v is an integer and v is inexact. E.g.,
```> (define v ...)
> (integer? v)
#t
> (exact? v)
#f
```

### Other Problems

Are there other problems you've encountered in recent labs?

## Program Design

### Using Whitespace

• Computers don't care.
• Humans do.
• So set up your code in a way that humans can more easily read it (and trace parantheses).

• Computers don't care.
• Humans do.
• So try to explain things to those who must use your code.
• Who will use your code?
• Folks who want to take advantage of your procedures without paying attention to how you implement them.
• Folks who have to fix your procedures when you get things wrong.
• Folks who want to do something similar.
• While you may understand your code perfectly now, you'll soon forget what you've done (even if you program regularly) and will eventually fall into many of these categories.
• So write lots of comments.

## History

Thursday, 29 August 2002

Wednesday, 11 September 2002

• Wrote most of the notes on the sample problems, based on observations from class.

Friday, 13 September 2002

• Added section on homework 1.
• Cleaned up slightly.

Back to Conditionals Lab. On to Characters and Strings.

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Mon Dec 2 08:40:57 2002.
The source to the document was last modified on Fri Sep 13 08:42:32 2002.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Outlines/outline.09.html`.

You may wish to validate this document's HTML ; ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu