General layout of the parts of Racket:
The Racket language is (mostly) in the Scheme family, or more generally in the Lisp family;
Racket: the core language implementation (language and runtime), written in C;
The actual language(s) that are available in Racket have lots of additional parts that are implemented in Racket itself;
GRacket: a portable Racket GUI extension, written in Racket too;
DrRacket: a GRacket application (also written in Racket);
Documentation: the Racket documentation is your friend (But beware that some things are provided in different forms from different places).
A review of “Goto Statement Considered Harmful”, by E.W. DIJKSTRA
This paper tries to convince us that the well-known goto statement should be eliminated from our programming languages or, at least (since I don’t think that it will ever be eliminated), that programmers should not use it. It is not clear what should replace it. The paper doesn’t explain to us what would be the use of the
ifstatement without a
gototo redirect the flow of execution: Should all our postconditions consist of a single statement, or should we only use the arithmetic
if, which doesn’t contain the offensive
And how will one deal with the case in which, having reached the end of an alternative, the program needs to continue the execution somewhere else?
The author is a proponent of the so-called “structured programming” style, in which, if I get it right, gotos are replaced by indentation. Structured programming is a nice academic exercise, which works well for small examples, but I doubt that any real-world program will ever be written in such a style. More than 10 years of industrial experience with Fortran have proved conclusively to everybody concerned that, in the real world, the goto is useful and necessary: its presence might cause some inconveniences in debugging, but it is a de facto standard and we must live with it. It will take more than the academic elucubrations of a purist to remove it from our languages.
Publishing this would waste valuable paper: Should it be published, I am as sure it will go uncited and unnoticed as I am confident that, 30 years from now, the goto will still be alive and well and used as widely as it is today.
Confidential comments to the editor: The author should withdraw the paper and submit it someplace where it will not be peer reviewed. A letter to the editor would be a perfect choice: Nobody will notice it there!
Racket syntax: similar to other Sexpr-based languages.
Reminder: the parens can be compared to C/etc function call parens —
they always mean that some function is applied. This is the reason why
(+ (1) (2)) won’t work: if you use C syntax that is
+(1(), 2()) but
1 isn’t a function so
1() is an error.
An important difference between syntax and semantics. A good way to
think about this is the difference between the string
42 stored in a
file somewhere (two ASCII values), and the number
42 stored in
memory (in some representation). You could also continue with the above
example: there is nothing wrong with “murder” — it’s just a word,
but murder is something you’ll go to jail for. The evaluation
function that Racket uses is actually a function that takes a piece of
syntax and returns (or executes) its semantics.
define expressions are used for creating new bindings, do not try to
use them to change values. For example, you should not try to write
(define x (+ x 1)) in an attempt to mimic
x = x+1.
It will not work.
There are two boolean values built in to Racket:
#t (true) and
(false). They can be used in
if statements, for example:
(< 2 3) evaluates to
#t. As a matter of fact, any value
#f is considered to be true, so:
Note: Racket is a functional language — so everything has a value.
This means that the expression
has no meaning when
test evaluates to
#f. This is unlike Pascal/C
where statements do something (side effect) like printing or an
assignment — here an
if statement with no alternate part will just
do nothing if the test is false… Racket, however, must return some
value — it could decide on simply returning
#f (or some unspecified
value) as the value of
as some implementations do, but Racket just declares it a syntax error.
(As we will see in the future, Racket has a more convenient
a clearer intention.)
Well, almost everything is a value…
There are certain things that are part of Racket’s syntax — for
define are special forms, they do not have a value!
More about this shortly.
(Bottom line: much more things do have a value, compared with other languages.)
cond is used for a
else if …
else if …
else … sequence.
The problem is that nested
ifs are inconvenient. For example,
In C/Java/Whatever, you’d write:
(Side question: why isn’t there a
return statement in Racket?)
But trying to force Racket code to look similar:
is more than just bad taste — the indentation rules are there for a reason, the main one is that you can see the structure of your program at a quick glance, and this is no longer true in the above code. (Such code will be penalized!)
So, instead of this, we can use Racket’s
cond statement, like this:
else is a keyword that is used by the
cond form — you
should always use an
else clause (for similar reasons as an
avoid an extra expression evaluation there, and we will need it when we
use a typed language). Also note that square brackets are read by
DrRacket like round parens, it will only make sure that the paren pairs
match. We use this to make code more readable — specifically, there
is a major difference between the above use of
 from the
conventional use of
(). Can you see what it is?
The general structure of a
Example for using an
if expression, and a recursive function:
Use this to show the different tools, especially:
An example of converting it to tail recursive form:
Additional notes about homework submissions:
Begin every function with clear documentation: a type followed by a purpose statement.
Document the function when needed, and according to the guidelines above and in the style guide.
After the function, always have a few test cases — they should cover your complete code (make sure to include possible corner cases). Later on, we will switch to testing the whole file through it’s “public interface”, instead of testing each function.