We are continuing our stroll through the Prologue of HtDP2e.
(When we have finished this stroll, you will know much of the language. Then we will start over and talk about a more systematic way of designing programs.)
Expressions are like little questions for Racket to answer. We have seen two ways to make expressions. An expression is either:
-
A value; or
-
(function expression1 expression2 ...)
The value of the first kind of expression is itself. To evaluate the second kind of expression, Racket first evaluates all the sub-expressions, then applies the function to the values produced.
We also now know several kinds of value. A value is either:
- A number (
42); - A string (
"Hello, World."); - A "Boolean" (either
#trueor#false); or - An image
And we know several built-in functions, for example:
- Arithmetic functions like
+,-,logand so on; - String functions like
string-append,string->numberand so on; - Boolean functions like
and,or, andnot; and - Image functions like
circle.
-
How many centimetres are there in a foot? (There are 2.54 centimetres in an inch, and 12 inches in a foot.)
-
How many letters are there in "Supercalifragilisticexpialidocious"? (Hint: remember there is a function
string-length) -
Look in the documentation for
string-ith. What is the 10th letter of "antidisestablishmentarianism"?The documentation is under "How to Design Programs Languages", under "1 Beginning Student".
-
Look in the documentation for
string<?. What is(string<? "hello" "goodbye")? -
What is
(and (or #true #false #false) (or #false #false))? (Try to guess before entering it into Racket.) -
Put the following in the definitions window, and click Run (or select Run from one of the menus, or click ctrl-R [or command-R on a Mac]).
(require 2htdp/image)Now try the next few. (Either in the interactions window or the definitions window.)
(circle 20 "outline" "orange")(rectangle 10 20 "solid" "brown")(triangle 20 "solid" "darkgreen")Try the following in the definitions window:
(require 2htdp/image)
(above
(triangle 30 "solid" "darkgreen")
(rectangle 10 20 "solid" "brown"))(From now on, I'll leave out the require.)
(beside
(above
(triangle 30 "solid" "darkgreen")
(rectangle 10 20 "solid" "brown"))
(above
(triangle 30 "solid" "darkgreen")
(rectangle 10 20 "solid" "brown")))What if I want lots of trees?
What if I want to make a tree of a given height?
Here is the definition of a function to convert inches to centimetres:
(define (inches->cm x)
(* x 2.54))- Fill in the missing part of this definition to define a function of one
argument,
feet->inches, that converts feet into inches:
(define (feet->inches x)
<missing>)Check that your function works. Eg,
(feet->inches 1)
should produce the number 12.
-
Define a function
feet->cmusing the functionsfeet->inchesandinches->cm. -
Define a constant called
CM/INCHwhose value is 2.54 and replace 2.54 in the defintion ofinches->cmwith this constant.
(4) The earth is 24000 miles around the equator. How many meters is that? (There are 1.6 kilometres in a mile).
(5) The speed of light is 3×108 metres per second. How long does it take light to go around the earth?