# Class 18: Side effects, sequencing, and output

Back to Vectors. On to Scheme without DrScheme.

Held Monday, September 25, 2000

Summary

Today we visit issues pertaining to procedures that may behave differently each time you run them.

Notes

• Are there any questions on exam 1?
• I'll also ask at the beginning of class on Tuesday and Wednesday.
• I'll be in my office most of Thursday.
• Just so you know, ``The best thing about Friday was having 151 cancelled'' is not a joy-inspiring comment.
• We're going to put off vectors for a little while.
• This week we're going to work on writing CGI scripts.
• Today's class will have a little bit different format. No reading; no precise lab examples. Just some mixed discussion and exercises.

Overview

• Impure functions and random
• Generating extra output
• Multiple steps

## Impure Functions

• The Scheme we've learned to date is pure in that procedures behave just like mathematical functions: Given the same arguments, they return the same value.
• But Scheme also supports impure programming, in which procedures might return return different values at different times.
• The simplest such procedure is `(random val)` which produces a ``random'' value between 0 and val-1, inclusive.

### Exercise 1: Rolling Two Dice

a. Define a procedure, `(roll)`, that simulates a die. It should return a number between 1 and 6.

b. Define a procedure, `(pair-a-dice)`, that produces the result of rolling two dice.

c. Define a procedure, `(count-sevens num-rolls)` that counts the number of sevens in the given number of rolls of a pair of dice dice.

d. Define a procedure, `(sevens-or-elevens)` that rolls two dice and returns true if they sum to seven and false otherwise.

## Side Effects

• In addition to behaving differently in different cases, procedures can also affect the world around them.
• The most typical side-effect is to display values. You can display values with `write`, `display`, and `newline`.

### Exercise 2. Simple Testing

Play with those three methods and see how they are similar and how they differ.

## Sequencing Statements

• Once you have side-effecting operations, you often want to say "Do this, then that, then that".
• Scheme usually lets you do multiple things by simply putting the things in sequence (typically in the body of a procedure).
```;;; do-some-stuff
;;; By Samuel A. Rebelsky
;;; Version 1.0 of 25 September 2000
;;; Purpose: Prints some simple output
;;; Pre: None
;;; Post: Output has been generated
;;; Produces: Nothing
(define do-some-stuff
(lambda ()
(display "Hello")
(newline)
(display "Goodbye")))
```

### Exercise 3: Playing with Sequences

a. Make sure that `do-some-stuff` works.

b. What do you think will happen if you try

```(list (do-some-stuff))
```

c. Try some other simple sequences of operations.

• Use `(read)`.
• How do you use the result? Pass it to another function.
• For example,
```;;; interactive-square-root
;;;   Prompts for a number and prints its square root
;;; By Samuel A. Rebelsky
;;; Version 1.0 of 25 September 2000
;;; Parameters: None
;;; Preconditions: None
;;; Postconditons: Has read a number from input.  Prints output.
;;; Produces: Nothing
(define interactive-square-root
(lambda ()
(display "Enter a number: ")

(define isr-helper
(lambda (val)
(display "The square root of ")
(display val)
(display " is ")
(display (sqrt val))
(newline)))
```
• You run this with
```> (interactive-square-root)
```

### Exercise 4: Computing Multiple Square Roots

a. Test this pair of procedures.

b. Make the pair of procedures repeatedly ask for a value and compute its square root. (Hint: use recursion.)

c. How do you stop your program?

d. Add a test that ensures that the value entered is a number and, if not, stops the program.

## History

Thursday, 24 August 2000

• Created as a blank outline.

Monday, 25 September 2000

• Filled in the details.

Back to Vectors. On to Scheme without DrScheme.

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.