[plt-scheme] Re: Htdp chapter 12 insertion sort
On Jun 2, 2009, at 3:40 PM, aditya shukla wrote:
> I thought there should be else in place of cons? and what's the use
> of cons? here.
One answer, as somebody else already said, is "the earlier you
discover an error, the better."
Another is that the cases "(empty? L)" and "(cons? L)" come directly
from the data definition "a list is either empty or a cons". I
prefer to use "else" only for "none of the above" situations, e.g. I
want to respond in three different ways to three specific strings,
and in a fourth way to "any other string".
Yet another answer: if the questions in your cond-clauses are all
mutually exclusive, they can be reordered without changing the
correctness of the functions. If one of them might include another
(e.g. "else" is equivalent to "true", which includes EVERYTHING),
reordering the cond-clauses can break the program.
Stephen Bloch
sbloch at adelphi.edu