The first
and rest
operators work only on
lists created with cons
, i.e., non-empty lists. So
(first empty)
first: expects type <non-empty list> as 1st arg: given empty
No DrScheme notes for this section.
Infinite Reductions
When writing recursive programs, it is easy to create an expression
that never produces a value. For example, consider the following
program:
(define (contains-doll? los) (cond [(empty? los) false] [else (cond [(symbol=? (first los) 'doll) true] [else (contains-doll? los)])]))
(contains-doll?_los)
instead of
(contains-doll?_(rest los))
. As a result, the evaluation
(contains-doll?_(cons 'ball empty))
proceeds as follows:
(contains-doll? (cons 'ball empty)) = (cond [(empty? (cons 'ball empty)) false] [else (cond [(symbol=? (first (cons 'ball empty)) 'doll) true] [else (contains-doll? (cons 'ball empty))])])) = (cond [(symbol=? (first (cons 'ball empty)) 'doll) true] [else (contains-doll? (cons 'ball empty))])])) = (contains-doll? (cons 'ball empty)) = ...
When DrScheme takes suspiciously long to evaluate an expression, click
the Break button and inspect your program. Look for a
missing rest
in a recursive call, or for some other bug that
makes it impossible to reduce an expression to a value.
Infinite Reductions using Infinite Memory
In the above example, (contains-doll?_(cons 'ball empty))
eventually reduces to itself. In a few bad programs, an expression
can ``reduce'' to an ever larger expression. For example, if
(contains-doll?_los)
is changed in the above program to
(add1 (contains-doll?_los))
, then the reduction steps
include
(contains-doll? (cons 'ball empty)) = ... = (add1 (contains-doll? (cons 'ball empty))) = ... = (add1 (add1 (contains-doll? (cons 'ball empty)))) = ...
Eventually, DrScheme would comsume all the memory on your computer, and then die. However, before this happens, you would probably notice that you machine's disk starts running constantly as DrScheme starts to use disk space for memory. If this happens, click the Break button as soon as possible.
Once DrScheme starts using disk space for memory, it might become addicted to using the disk, so that DrScheme continues to use the disk even after recovering from Break. If that happens, quit DrScheme (in the normal way, using the File|Quit menu) and start over to break DrScheme's addiction and get better performance.
No DrScheme notes for this section.
No DrScheme notes for this section.