# Class 33: Pause for Breath

Back to Input and Output. On to Files.

Held: Thursday, 30 October 2003

Summary: Today we pause to enjoy the wonderful weather.

Assignments

Notes:

• I am somewhat discombobulated today. I'll explain why in class. The main effect that you'll note is that preparation may be less good than normal.
• We will have time for you to ask questions on the exam.

## 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, 28 August 2003 [Samuel A. Rebelsky]

• Created automatically.

Back to Input and Output. On to Files.

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 Tue Dec 9 13:59:50 2003.
The source to the document was last modified on Mon Sep 1 13:30:51 2003.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003F/Outlines/outline.33.html`.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu