CSE 341 -- Side Effects in Scheme

When To Use Side Effects in Scheme?

I/O

Scheme includes various input and output statements, including terminal-based I/O and file I/O.

Some I/O functions:

read is a function that reads one Scheme object from the standard input and returns it. (Note that this is thus a function with a side effect.)

Example:

(define clam (read))
There are some other functions to to read characters without parsing them into Scheme objects.

write prints a representation of its argument to the standard output.

Example. This outputs a 7:

(write (+ 3 4))

Multiple Forms

Many of the constructs we've discussed so far, including define, let, and cond, allow multiple forms in their bodies. All but the last form is evaluated just for its side effect, and the value of the last form is used. For example:
(define (testit x)
  (write "this function doesn't do much")
  (newline)
  (write "but it does illustrate the point")
  (newline)
  (+ x x))
Here the write and newline expressions are evaluated (for their effect) and then the last expression (+ x x) is evaluated and its value returned as the value of the function. Similarly, there can be multiple forms in the body of a let, or the consequent part of a clause in a cond. However, if must have just a single form in its two arms. (Otherwise, how would Scheme tell which was which?)

This capability hasn't been of any use in the past, since we haven't had any side effects -- clearly, the only time one would want to use this is if the forms other than the last have some side effect.


Assignment and List Surgery

Scheme includes a special form for assignment:
 (set! var expr) 
Example:
(define x 10)
(write x)
(set! x (+ x 1))
(write x)
There are also functions set-car! and set-cdr! to assign into the car or cdr of a cons cell. Finally, there is a special form do for iteration.

Example:

(define x '(1 2 3 4))
(write x)     ; prints (1 2 3 4)
(set-car! x 100)
(write x)     ; prints (100 2 3 4)
(set-cdr! x ())
(write x)     ; prints  (100)
Making a circular list:
(define circ '(a b))
(set-cdr! (cdr circ) circ)
When you pass a list to a function in Scheme, a reference to the list is passed, rather than a copy. Thus, if you use set-car! or set-cdr! to do list surgery inside the function, the actual parameter will be affected.
(define (change-it x)
    (set-car! x 'frog))

(define animals '(rabbit horse squirrel monkey))
(change-it animals)
However, consider:
(define (test x)
   (write x)
   (set! x 100)
   (write x))

(define y '(1 2 3))
(test y)  ; prints (1 2 3) then 100
(write y) ; prints (1 2 3) ... y is unaltered