Computational Thinking :   CSC104 (Fall 2017)
1 The Definitions and Interactions Areas
1.1 Inserting Comments
2 Intermediate Steps Computing an Expression
step
steps
3 Images
3.1 Literal Images
3.1.1 Copying Images in Dr  Racket
bitmap/  url
3.2 Combining Images
beside
above
overlay
beside/  align
above/  align
overlay/  align
3.3 Rotating, Scaling, Flipping, and Cropping
rotate
rotate-ccw
rotate-180
rotate-cw
scale
flip-horizontal
flip-vertical
crop-bottom
crop-top
crop-right
crop-left
3.4 Image Properties
image-width
image-height
3.5 Geometric Shapes
circle
square
triangle
rectangle
ellipse
star
3.5.1 Color
image-color?
3.5.2 Pixels
freeze
image->colors
colors->image
colors?
3.6 Image Functions Documented in Other Sections
4 Numbers
4.1 Arithmetic
+
*
-
/
add1
sub1
sqr
square-root
power
pi
4.1.1 Whole-Number Division
quotient
remainder
4.2 Rounding
round
floor
ceiling
4.3 Comparing Numbers
=
<
>
<=
>=
maximum
minimum
4.4 Types of Numbers
integer?
natural?
negative?
zero?
non-negative?
positive?
even?
odd?
4.5 Generating Random Numbers
random
4.6 Number Functions Documented in Other Sections
5 Definitions
define
5.1 Temporary Definitions
local
6 Strings
string-length
string-append
substring
character?
string-ref
string-contains?
6.1 String Functions Documented in Other Sections
6.2 Types of Characters
string-alphabetic?
string-numeric?
string-upper-case?
string-lower-case?
string-whitespace?
6.3 Case Conversion
string-upper-case
string-lower-case
6.4 Alphabetic Order Comparisons
string<?
string>?
string<=?
string>=?
string-ci<?
string-ci>?
string-ci<=?
string-ci>=?
6.5 Unicode Numbering of Characters
character->unicode
unicode->character
7 Equality
equal?
image=?
string=?
boolean=?
7.1 Approximate Equality
string-ci=?
=~
equal~?
8 Testing
check-expect
time
9 Booleans
not
9.1 Conditional Expressions
and
or
cond
else
9.2 Boolean Functions Documented in Another Section
10 Types of Data
10.1 Showing Data Types in Dr  Racket
10.2 Type Predicates
image?
number?
string?
boolean?
list?
function?
10.3 Converting Between Types
number->string
string->number
string->image
string->list
11 Lists
list
length
first
rest
empty?
second
third
fourth
list-ref
reverse
list*
append
range
map
apply
repeated
filter
sort
make-list
member?
remove
remove-all
assoc
11.1 List Functions Documented in Other Sections
12 Animation
big-bang
13 Miscellaneous
identity
current-seconds
14 Packages/  Libraries
require
7.0

Computational Thinking : CSC104 (Fall 2017)

Gary Baumgartner <[email protected]>

This page is a reference for the University of Toronto “Computational Thinking” Teaching Language, used in the Fall 2017 offering of CSC 104.

    1 The Definitions and Interactions Areas

      1.1 Inserting Comments

    2 Intermediate Steps Computing an Expression

    3 Images

      3.1 Literal Images

        3.1.1 Copying Images in DrRacket

      3.2 Combining Images

      3.3 Rotating, Scaling, Flipping, and Cropping

      3.4 Image Properties

      3.5 Geometric Shapes

        3.5.1 Color

        3.5.2 Pixels

      3.6 Image Functions Documented in Other Sections

    4 Numbers

      4.1 Arithmetic

        4.1.1 Whole-Number Division

      4.2 Rounding

      4.3 Comparing Numbers

      4.4 Types of Numbers

      4.5 Generating Random Numbers

      4.6 Number Functions Documented in Other Sections

    5 Definitions

      5.1 Temporary Definitions

    6 Strings

      6.1 String Functions Documented in Other Sections

      6.2 Types of Characters

      6.3 Case Conversion

      6.4 Alphabetic Order Comparisons

      6.5 Unicode Numbering of Characters

    7 Equality

      7.1 Approximate Equality

    8 Testing

    9 Booleans

      9.1 Conditional Expressions

      9.2 Boolean Functions Documented in Another Section

    10 Types of Data

      10.1 Showing Data Types in DrRacket

      10.2 Type Predicates

      10.3 Converting Between Types

    11 Lists

      11.1 List Functions Documented in Other Sections

    12 Animation

    13 Miscellaneous

    14 Packages/Libraries

1 The Definitions and Interactions Areas

The Interactions Area is where you can enter an expression and have DrRacket immediately compute its value.

When you press Return (which might be labelled “Enter” on your keyboard), DrRacket checks whether you have written a complete expression (essentially: are all the parentheses “balanced”). If not, DrRacket simply moves the cursor to a new line, so you can complete or edit the expression you are creating. If you have written a complete expression, then pressing Return makes DrRacket compute the result of the expression and show you its value.

If you are editing an expression and want to spread it over multiple lines while you work on it (something you will see us do for more complex expressions to show their structure), then you can hold down the Shift key while you press Return. Holding down the Shift key prevents DrRacket from computing the expression, even if the expression looks complete.

1.1 Inserting Comments

There are four(!) ways to write something in your code so that what you write will be ignored by DrRacket when running the document. Something written for a human reader of your code, not for DrRacket to interpret, is called a “comment”.

A semi-colon “;” makes DrRacket ignore everything from it up to the end of the line it’s on, and the text will be given a different color than code. You can put a semi-colon at the start of many lines at once, by selecting multiple lines and then choosing the menu item “Racket / Commment Out with Semicolons” (turning something into a comment is called “commenting it out”, and this kind of comment is called an “end-of-line comment”). If you have many lines with semi-colons at the start, you can remove them all by selecting the lines and then choosing the menu item “Racket / Uncomment”

A comment box creates an area where text is still colored like code but otherwise ignored by DrRacket. You can insert one with the menu item "Insert / Insert Comment Box", and then type inside it. You can also comment out a part of your document by selecting it and then choosing the menu item "Racket / Comment Out with a Box".

Another way to comment out a part of your document is to surround it with the delimiting pairs of characters “#|” and “|#”. Like with semi-colons, the color will change. This kind of comment is called a “block comment”.

Finally, to make DrRacket ignore a definition or expression, you can put the two characters “#;” in front of the expression. This can be a little hard to notice at first, since of the definition or expression doesn’t change.

2 Intermediate Steps Computing an Expression

To see the steps to compute an expression, you can use DrRacket’s traditional Stepper, by pressing the “Step” button. To see a listing of all the steps, closer to our course’s style, use one of the following step or steps forms.

special form

(step expression)

Print one step of computation, if there is one.

special form

(steps expression/definition
       ...)
Print all steps of computation.

3 Images

3.1 Literal Images

A “literal” image (as opposed to one computed by an image function) can be entered into DrRacket, and used as-is as a value in code.

3.1.1 Copying Images in DrRacket

You can find functions for modifying images, as well as creating geometric shapes, in later sections. But if you find an image that you would like to include, you can copy it into DrRacket from another application, from a file on your computer, or from a web address.

function

(bitmap/url url)  image?

  url : string?
The image at the web address url.
> (bitmap/url "https://racket-lang.org/logo.png")

tcp-connect: connection failed;

 host not found

  address: racket-lang.org

  port number: 443

  system error: Name or service not known; gai_err=-2

You can also save an image from DrRacket to a file, using the image context menu (right-click on the image):

3.2 Combining Images

function

(beside an-image ...)  image?

  an-image : image?
The single image formed by arranging all the images in a horizontal row, vertically centered, starting with the first image at the left.
> (beside image image image)

image

function

(above an-image ...)  image?

  an-image : image?
The single image formed by arranging all the images in a vertical column, horizontally centered, starting with the first image at the top, above the second image, above the third image, etc.
> (above image image image)

image

function

(overlay an-image ...)  image?

  an-image : image?
The single image formed by arranging all the images in the same location, centered, with the first image on the second image, on the third image, etc.
> (overlay image image)

image

> (overlay image image image image image)

image

function

(beside/align alignment an-image ...)  image?

  alignment : (or/c "top" "bottom" "center" "baseline")
  an-image : image?
(above/align alignment an-image ...)  image?
  alignment : (or/c "left" "right" "center")
  an-image : image?
The image formed by placing the images beside, or above, each other, but aligned according to alignment instead of automatically centered.
> (beside/align "top" image image image)

image

> (above/align "left" image image image)

image

The alignment "baseline" is meant for images produced by the string->image function (from section Converting Between Types). Using it lines up the character “baselines”, ignoring descending parts of letters like "p":
> (beside/align "baseline"
                (string->image "play" 30 "tomato")
                (string->image "a" 40 "blue")
                (string->image "jig" 20 "darkgreen"))

image

function

(overlay/align horizontal-alignment    
  vertical-alignment    
  an-image ...)  image?
  horizontal-alignment : (or/c "left" "right" "center")
  vertical-alignment : (or/c "top" "bottom" "center" "baseline")
  an-image : image?
The image formed by arranging all the images in the same location, but aligned according to horizontal-alignment and vertical-alignment instead of automatically centered, with the first image on the second image, on the third image, etc.

3.3 Rotating, Scaling, Flipping, and Cropping

function

(rotate angle an-image)  image?

  angle : number?
  an-image : image?
(rotate-ccw an-image)  image?
  an-image : image?
(rotate-180 an-image)  image?
  an-image : image?
(rotate-cw an-image)  image?
  an-image : image?
The image, rotated angle degrees counter-clockwise, 90 degrees counter-clockwise, 180 degrees, or 90 degrees clockwise, around its center.
> (rotate 45 image)

image

> (rotate -10 image)

image

> (define a-half-triangle (crop-right (triangle 40 "solid" "forestgreen")
                                      20))
> a-half-triangle

image

> (rotate-ccw a-half-triangle)

image

> (rotate-180 a-half-triangle)

image

> (rotate-cw a-half-triangle)

image

function

(scale factor an-image)  image?

  factor : (and/c number? positive?)
  an-image : image?
The image, scaled in both directions by the amount factor.
> (scale 2 image)

image

function

(flip-horizontal an-image)  image?

  an-image : image?
(flip-vertical an-image)  image?
  an-image : image?
The image, mirrored left-to-right, or flipped upside-down.
> (beside (rotate -30 image) (flip-horizontal (rotate -30 image)))

image

> (above image (flip-vertical image))

image

The image cannot contain an image created with the string->image function (from section Converting Between Types): using flip-horizontal or flip-vertical with such an image reports an error.

function

(crop-bottom an-image pixels)  image?

  an-image : image?
  pixels : (and/c number? non-negative?)
(crop-top an-image pixels)  image?
  an-image : image?
  pixels : (and/c number? non-negative?)
(crop-right an-image pixels)  image?
  an-image : image?
  pixels : (and/c number? non-negative?)
(crop-left an-image pixels)  image?
  an-image : image?
  pixels : (and/c number? non-negative?)
A version of the image, with the specified number of pixels removed from the top, bottom, left, or right of the image.

If the number of pixels is too many for the size of the image, an error is reported.
> (crop-bottom image 9)

image

> (above image (crop-top image 13))

image

> (beside (crop-right image 20) (crop-left image 10))

image

3.4 Image Properties

function

(image-width an-image)  natural?

  an-image : image?
(image-height an-image)  natural?
  an-image : image?
Width, or height, of an image.
> (image-width (rectangle 5 30 "solid" "purple"))

5

> (image-height (rectangle 5 30 "solid" "purple"))

30

> (image-width (circle 200 "solid" "orange"))

400

> (image-height (above (circle 200 "solid" "orange")
                       (rectangle 5 30 "solid" "purple")))

430

3.5 Geometric Shapes

function

(circle radius mode color)  image?

  radius : (and/c number? non-negative?)
  mode : (or/c "solid" "outline")
  color : image-color?
A circle with the given radius, mode, and color.
> (circle 15 "outline" "green")

image

> (circle 10 "solid" "blue")

image

function

(square side-length mode color)  image?

  side-length : (and/c number? non-negative?)
  mode : (or/c "solid" "outline")
  color : image-color?
(triangle side-length mode color)  image?
  side-length : (and/c number? non-negative?)
  mode : (or/c "solid" "outline")
  color : image-color?
A square, or upward-pointing triangle, with the given side length, mode, and color.
> (square 20 "solid" "purple")

image

> (square 30 "outline" "brown")

image

> (triangle 30 "solid" "darkgreen")

image

function

(rectangle width height mode color)  image?

  width : (and/c number? non-negative?)
  height : (and/c number? non-negative?)
  mode : (or/c "solid" "outline")
  color : image-color?
(ellipse width height mode color)  image?
  width : (and/c number? non-negative?)
  height : (and/c number? non-negative?)
  mode : (or/c "solid" "outline")
  color : image-color?
A rectangle, or an ellipse, with the given width, height, mode, and color.
> (rectangle 40 20 "outline" "black")

image

> (rectangle 20 40 "solid" "aquamarine")

image

> (ellipse 40 20 "outline" "maroon")

image

> (ellipse 20 40 "solid" "red")

image

function

(star distance mode color)  image?

  distance : (and/c number? non-negative?)
  mode : (or/c "solid" "outline")
  color : image-color?
A five-pointed star, with the points distance apart, with the given mode and color.
> (star 20 "solid" "yellow")

image

3.5.1 Color

function

(image-color? v)  boolean?

  v : any/c
Is the argument a string considered the name of a color, or a list of three or four percentages (numbers between 0 and 100 inclusive).
> (image-color? (list 12 56 34))

#true

> (image-color? (list 0 100 0 100))

#true

> (image-color? "orange")

#true

> (image-color? "ORANGE")

#true

> (image-color? "Black")

#true

> (image-color? "bloo")

#false

> (image-color? "transparent")

#true

The list of color names is shown in color-database<%>, except "transparent" is also considered a color. Color naming is case-insensitive (see string-ci=? in Approximate Equality).

A list of three or four percentages represents a color with that percentage of red, green, blue, respectively, and optionally how opaque the color is (which defaults to 100 if unspecified).

3.5.2 Pixels

function

(freeze image)  image?

  image : image?
The image, but without the hidden information about how it was constructed. This can improve the drawing speed, but loses information that can make the image look better if it is rotated or scaled up (if the original was scaled down).

function

(image->colors image)  colors?

  image : image?
A list of the colors of the pixels in the image, row by row, with each color represented by a list of four percentages (see image-color? for the meaning of the percentages).

function

(colors->image colors width height)  image?

  colors : colors?
  width : natural?
  height : natural?
An image built from the list of colors, each color represented by a list of four percentages, (see image-color? for the meaning of the percentages) arranged row by row inside a rectangle of the given width and height.

function

(colors? v)  boolean?

  v : any/c
Is the argument a list of lists of three or four numbers, where each numbers is a percentage (a number between 0 and 100 inclusive).

3.6 Image Functions Documented in Other Sections

function

(string->image string font-size color)  image?

  string : string?
  font-size : (and/c natural? (<=/c 1 255))
  color : image-color?

function

(image? v)  boolean?

  v : any/c

function

(image=? image-1 image-2)  boolean?

  image-1 : image?
  image-2 : image?

4 Numbers

4.1 Arithmetic

function

(+ x ...)  number?

  x : number?
Sum of the numbers (the numbers added together).
> (+ 100 -1 5)

104

> (+ 1/3 1/3)

2/3

> (+ 7)

7

There is a convention in CS (and Math) that the sum of no numbers is 0:
> (+)

0

function

(* x ...)  number?

  x : number?
Product of the numbers (the numbers multiplied together).
> (* 5 -2 3)

-30

> (* 3 4/3)

4

> (* 7)

7

There is a convention in CS (and Math) that the product of no numbers is 1:
> (*)

1

function

(- x)  number?

  x : number?
(- y x)  number?
  y : number?
  x : number?
With one argument: the negative of the number.
> (- 7)

-7

With two arguments: the difference of the two numbers (the first number minus the second number; also phrased as: the second number subtracted from the first number).
> (- 7 2)

5

function

(/ x y)  number?

  x : number?
  y : number?
The first number divided by the second number (the first number over the second number).
> (/ 12 2)

6

function

(add1 x)  number?

  x : number?
(sub1 x)  number?
  x : number?
One more, or one less, than the number.
> (add1 3)

4

> (sub1 3)

2

Incrementing (adding 1) and decrementing (subtracting 1) are somewhat common in programming. Using names for them is intended to make it easier for someone reading code to see at a glance which of the two operations is intended.

If add1 and sub1 were not already in the language, they could be defined this way:
(define (add1 x)
  (+ x 1))
(define (sub1 x)
  (- x 1))

function

(sqr x)  number?

  x : number?
Square of the number.
> (sqr 5)

25

If sqr was not already in the language, it could be defined this way:
(define (sqr x)
  (* x x))
The name is abbreviated to avoid clashing with the square function that produces an image of a square.

function

(square-root x)  number?

  x : (and/c number? non-negative?)
Square root of the number.
> (square-root 9)

3

> (square-root 2)

#i1.4142135623730951

function

(power base exponent)  number?

  base : (and/c number? positive?)
  exponent : number?
The first number (the “base”) raised to the power of the second number (the “exponent”).
> (power 3 2)

9

> (power 10 3)

1000

value

pi : number?

The ratio of a circle’s circumference to its diameter.
> pi

#i3.141592653589793

4.1.1 Whole-Number Division

Whole-number division, also called “integer division”, is a version of division (/) If you are not familiar with the terms “integer” and “natural number”, see Types of Numbers. that produces a whole number “quotient” along with any leftover “remainder”.

There is no agreement on the meaning of whole-number division for negative numbers, so for simplicity this course provides and uses it only for natural numbers.

function

(quotient m n)  natural?

  m : natural?
  n : (and/c natural? positive?)
(remainder m n)  natural?
  m : natural?
  n : (and/c natural? positive?)
The first number divided by the second number, rounded down to the nearest natural number, and the remainder.
> (quotient  17 5) ; Since 5 “goes into” 17 three times,

3

> (remainder 17 5) ; with two “left over”.

2

If quotient was not already in the language, it could be defined this way:
(define (quotient m n)
  (floor (/ m n)))

4.2 Rounding

function

(round x)  integer?

  x : number?
(floor x)  integer?
  x : number?
(ceiling x)  integer?
  x : number?
The integer that is closest to the number (round), closest and less than or equal to the number (floor, also known as “round down”), or closest and greater than or equal to the number (ceiling, also known as “round up”).
> (round 16)

16

> (round 16.3)

16

> (round 16.8)

17

> (round 17)

17

> (floor 16)

16

> (floor 16.3)

16

> (floor 16.8)

16

> (floor 17)

17

> (ceiling 16)

16

> (ceiling 16.3)

17

> (ceiling 16.8)

17

> (ceiling 17)

17

4.3 Comparing Numbers

See also non-negative?, positive?, negative?, and zero? in Types of Numbers.

function

(= x y)  boolean?

  x : number?
  y : number?
(= x y z ...)  boolean?
  x : number?
  y : number?
  z : number?
With two arguments: are the two numbers equal?
> (= 2 3)

#false

> (= 3 3)

#true

With more than two arguments: are all the numbers equal to each other?
> (= 3 3 2 3)

#false

> (= 3 3 3 3 3)

#true

See also Equality for a discussion of equal?, which works both on numbers and other types of data.

function

(< x y)  boolean?

  x : number?
  y : number?
(< x y z ...)  boolean?
  x : number?
  y : number?
  z : number?
With two arguments: is the first number less (smaller) than the second number?
> (< 2 3)

#true

> (< 3 3)

#false

> (< 3 2)

#false

With more than two arguments: are the numbers in increasing order (is each number less than the next number)?
> (< 2 3 5 7)

#true

> (< 2 3 3 5 7)

#false

> (< 2 5 3 7)

#false

function

(> x y)  boolean?

  x : number?
  y : number?
(> x y z ...)  boolean?
  x : number?
  y : number?
  z : number?
With two arguments: is the first number greater (larger) than the second number?
> (> 2 3)

#false

> (> 3 3)

#false

> (> 3 2)

#true

With more than two arguments: are the numbers in decreasing order (is each number greater than the next number)?
> (> 7 5 3 2)

#true

> (> 7 5 3 3 2)

#false

> (> 7 3 5 2)

#false

function

(<= x y z ...)  boolean?

  x : number?
  y : number?
  z : number?
(>= x y z ...)  boolean?
  x : number?
  y : number?
  z : number?
Is each number less than or equal, or greater than or equal, to the next number?
> (<= 2 3)

#true

> (<= 3 3)

#true

> (<= 3 2)

#false

> (<= 2 3 3 3 5 7 7)

#true

> (<= 2 5 3 7)

#false

> (>= 2 3)

#false

> (>= 3 3)

#true

> (>= 3 2)

#true

> (>= 7 7 5 3 3 3 2)

#true

> (>= 7 3 5 2)

#false

function

(maximum x y ...)  number?

  x : number?
  y : number?
(minimum x y ...)  number?
  x : number?
  y : number?
Maximum (largest, greatest), or minimum (smallest, least), of the given numbers.
> (maximum 3 2 7 9 8)

9

> (minimum 3 2 7 9 8)

2

4.4 Types of Numbers

The predicate number? in Types of Data asks whether a value is a number. If you have not learned the term “predicate” yet: it means a function that produces a boolean. If you have not learned the term “boolean” yet: it means one of the special values #true or #false. Predicates in this section determine whether a value is a particular kind of number.

function

(integer? v)  boolean?

  v : any/c
(natural? v)  boolean?
  v : any/c
(integer? v): is v an integer (one of the the numbers 0, -1, 1, -2, 2, -3, ...)?

(natural? v): is v a natural number (one of the numbers 0, 1, 2, 3, ...)?

> (integer? "hello")

#false

> (natural? "hello")

#false

> (integer? 3/2)

#false

> (natural? 3/2)

#false

> (integer? 2)

#true

> (natural? 2)

#true

> (integer? -2)

#true

> (natural? -2)

#false

> (integer? 0)

#true

> (natural? 0)

#true

If natural? was not already in the language, it could be defined this way:
(define (natural? v)
  (and (integer? v) (non-negative? v)))

function

(negative? x)  boolean?

  x : number?
(zero? x)  boolean?
  x : number?
(non-negative? x)  boolean?
  x : number?
(positive? x)  boolean?
  x : number?
(negative? x): is the number x less than zero?

(zero? x): is the number x equal to zero?

(non-negative? x): is the number x at least zero?

(positive? x): is the number x greater than zero?

> (negative? -2)

#true

> (negative? 0)

#false

> (negative? 2)

#false

> (zero? -2)

#false

> (zero? 0)

#true

> (zero? 2)

#false

> (non-negative? -2)

#false

> (non-negative? 0)

#true

> (non-negative? 2)

#true

> (positive? -2)

#false

> (positive? 0)

#false

> (positive? 2)

#true

Notice these predicates expect a number (and report an error otherwise).

If these functions were not already in the language, they could be defined this way:
(define (negative? x)
  (< x 0))
(define (zero? x)
  (= x 0))
(define (non-negative? x)
  (>= x 0))
(define (positive? x)
  (> x 0))
See also =, <, >, <=, and >= in Comparing Numbers, and equal? in Equality.

function

(even? i)  boolean?

  i : integer?
(odd? i)  boolean?
  i : integer?
Notice these predicates expect an integer (and report an error otherwise).

(even? i): is integer i even (a multiple of 2)?

(odd? i): is integer i odd (not a multiuple of 2)?

> (even? 6)

#true

> (odd? 6)

#false

> (even? 7)

#false

> (odd? 7)

#true

See also remainder in Whole-Number Division.

4.5 Generating Random Numbers

function

(random n)  natural?

  n : (and/c natural? positive?)
A random natural number less than the number.
> (random 104)

102

> (random 104)

2

> (random 104)

5

4.6 Number Functions Documented in Other Sections

function

(number? v)  boolean?

  v : any/c

function

(number->string x)  string?

  x : number?
(string->number s)  (or/c number? #false)
  s : string?

5 Definitions

special form

(define name expression)

Define a variable, name, with the value of the expression.

The variable cannot have the same name as another variable or a function, and the variable cannot be used inside expression.

special form

(define (name parameter-name parameter-name ...)
        expression)
Define a function, name, with the “parameters” parameter-name ..., and the “body” expression.

When the function is called, the values of the arguments are inserted into the body in place of the parameter-names, and the function produces the value of that new expression.

The function cannot have the same name as another function or a variable.

5.1 Temporary Definitions

special form

(local [definition ...]
  expression)
Produce the value of expression (the “body”), in the context of some temporary definitions.str

Each definition must be a define form.

Each definition occurs, and then the result of the body expression is computed and used as the result of the whole local expression.

The definitions last only while the local result is being produced, and the names they define can only be referred to within the local. The definitions can define a name that already has a definition outside the local, in which case the local definition takes precedence inside the local.

6 Strings

A string is used to represent text. It is a sequence of characters, entered by surrounding the characters with double-quotes. The double-quotes are not considered part of the text itself, or put another way: they are not characters in the string itself. The double-quotes are just there to know where the text starts and ends, so the characters they surround are not interpreted as code.

There are various special “characters” in computing, for example the character to represent the end of a line in text that spans multiple lines. And putting a double-quote itself inside a string as one of its characters requires some way to indicate that the double-quote is not just signalling the end of the string. We avoid these technicalities, except if they become necessary or useful for a particular text processing task. If they come up naturally we will address them in that context, so except where we explicitly state otherwise, you can safely assume none of our strings will contain double-quotes as characters, nor span multiple lines.

function

(string-length s)  natural?

  s : string?
The number of characters in the string.
> (string-length "hello world")

11

function

(string-append s ...)  string?

  s : string?
A single string formed by joining the given strings.
> (string-append "hello" " " "world" ", " "good bye")

"hello world, good bye"

> (string-append "hello")

"hello"

> (string-append)

""

function

(substring s i j)  string?

  s : string?
  i : natural?
  j : natural?
The part of the string s starting from the character at index i (counting from zero), up to but not including the character at index j.
> (substring "hello world" 1 7)

"ello w"

> (substring "hello world" 2 2)

""

The index i must be less than or equal to the index j, which must be less than or equal to the length of the string s, otherwise an error is reported.

function

(character? v)  boolean?

  v : any/c
Is the argument a string whose length is one?
> (character? 1)

#false

> (character? "a")

#true

> (character? "abc")

#false

function

(string-ref s i)  character?

  s : string?
  i : natural?
The character in the string at the given index (counting from zero).
> (string-ref "hello world" 1)

"e"

If string-ref was not in the language, it could be defined this way:
(define (string-ref s i)
  (substring s i (add1 i)))

function

(string-contains? s t)  boolean?

  s : string?
  t : string?
Does the first string appear inside the second one?
> (string-contains? "it" "kitten")

#true

6.1 String Functions Documented in Other Sections

function

(string? v)  boolean?

  v : any/c

function

(string=? s t u ...)  boolean?

  s : string?
  t : string?
  u : string?
See Equality.

function

(string->number s)  (or/c number? #false)

  s : string?
(number->string x)  string?
  x : number?

function

(string->list s)  (listof character?)

  s : string?

6.2 Types of Characters

function

(string-alphabetic? s)  boolean?

  s : string?
Does the string contain only letters?
> (string-alphabetic? "Cat")

#true

> (string-alphabetic? "Cat?")

#false

> (string-alphabetic? "123")

#false

function

(string-numeric? s)  boolean?

  s : string?
Does the string contain only digits?
> (string-numeric? "123")

#true

> (string-numeric? "-23")

#false

function

(string-upper-case? s)  boolean?

  s : string?
(string-lower-case? s)  boolean?
  s : string?
Does the string contain only upper-case letters, or only lower-case letters?
> (string-upper-case? "CAT")

#true

> (string-upper-case? "kITTY")

#false

> (string-upper-case? "CAT?")

#false

> (string-lower-case? "cat")

#true

> (string-upper-case? "kITTY")

#false

> (string-lower-case? "cat?")

#false

function

(string-whitespace? s)  boolean?

  s : string?
Does the string contain only blanks (spaces, or other special “invisible” characters)?
> (string-whitespace? "  ")

#true

> (string-whitespace? " a ")

#false

> (string-whitespace? "\n")

#true

6.3 Case Conversion

function

(string-upper-case s)  string?

  s : string?
(string-lower-case s)  string?
  s : string?
The upper, or lower, case version of the string.
> (string-upper-case "kIttY!")

"KITTY!"

> (string-lower-case "kIttY!")

"kitty!"

6.4 Alphabetic Order Comparisons

These functions compare two or more strings alphabetically.

Upper-case letters are considered alphabetically before lower-case letters in string<?, string>?, string<=?, and string>=? (for historical reasons). See also string=? in Equality.

The versions string<?, string>?, string<=?, and string>=? ignore the distinction between upper and lower-case (the “ci” in the name stands for “Case Insensitive”). See also string-ci=? in Equality, and also string-upper-case? and string-lower-case? in Types of Characters.

function

(string<? s t u ...)  boolean?

  s : string?
  t : string?
  u : string?
(string>? s t u ...)  boolean?
  s : string?
  t : string?
  u : string?
Are the strings in alphabetic order, or in reverse alphabetic order?
> (string<? "Beth" "al" "beth" "dee")

#true

> (string>? "zebra" "koala" "kitten" "Zebra")

#true

function

(string<=? s t u ...)  boolean?

  s : string?
  t : string?
  u : string?
(string>=? s t u ...)  boolean?
  s : string?
  t : string?
  u : string?
Allowing repeats, are the strings in alphabetic order, or in reverse alphabetic order?
> (string<=? "Beth" "al" "beth" "dee" "dee")

#true

> (string>=? "zebra" "koala" "koala" "kitten" "Zebra")

#true

function

(string-ci<? s t u ...)  boolean?

  s : string?
  t : string?
  u : string?
(string-ci>? s t u ...)  boolean?
  s : string?
  t : string?
  u : string?
(string-ci<=? s t u ...)  boolean?
  s : string?
  t : string?
  u : string?
(string-ci>=? s t u ...)  boolean?
  s : string?
  t : string?
  u : string?
Are the strings in a particular alphabetic order, ignoring upper versus lower case?
> (string-ci<? "al" "Beth" "dee")

#true

> (string-ci>? "Zebra" "koala" "kitten")

#true

> (string-ci<=? "al" "beth" "Beth" "dee")

#true

> (string-ci>=? "zebra" "zebra" "Koala" "kitten")

#true

6.5 Unicode Numbering of Characters

function

(character->unicode s)  natural?

  s : character?
(unicode->character n)  character?
  n : natural?
The natural number representing a character, and vice-versa, according to the the Unicode encoding for characters.
> (character->unicode "A")

65

> (unicode->character 65)

"A"

> (character->unicode "⚘")

9880

> (unicode->character 9880)

"⚘"

7 Equality

The function that compares whether two values are the same is equal?, which works with any type of value.

There are also restricted versions of equal?, that work only on values of a particular type, and report an error otherwise. In situations where you expect values to be of a particular type, then using the restricted versions of equal? can be clearer to someone reading your code. Using these versions can also report some errors earlier (ones where your code produced values of the wrong type, and then compares them) and more closely related to the true source, making them more useful.

See = for the version of equal? restricted to numbers.

function

(equal? v w)  boolean?

  v : any/c
  w : any/c
Are the two values the same?
> (equal? (list 1 2) (list (sub1 2) (+ 1 1)))

#true

function

(image=? image-1 image-2)  boolean?

  image-1 : image?
  image-2 : image?
Are the two images equal?
> (image=? image image)

#false

> (image=? image image)

#true

function

(string=? s t u ...)  boolean?

  s : string?
  t : string?
  u : string?
Are all the strings equal?

See also string-ci=? in Approximate Equality for case-insensitive comparison.

> (string=? "hello" "world")

#false

> (string=? "bye" "bye")

#true

function

(boolean=? b1 b2)  boolean?

  b1 : boolean?
  b2 : boolean?
Are the two booleans equal?
> (boolean=? #true #false)

#false

7.1 Approximate Equality

function

(string-ci=? s t u ...)  boolean?

  s : string?
  t : string?
  u : string?
Are all the strings equal, ignoring the distinction between upper and lower case?
> (string-ci=?  "hello" "HellO")

#true

function

(=~ x y z)  boolean?

  x : number?
  y : number?
  z : (and/c number? non-negative?)
Are numbers x and y within z of each other?
> (=~ 1.01 1.0 0.1)

#true

> (=~ 1.01 1.5 0.1)

#false

function

(equal~? v w z)  boolean?

  v : any/c
  w : any/c
  z : (and/c number? non-negative?)
Are v and w approximately the same: the same if numbers in v and w are considered the same when they are within z of each other?
> (equal~? (list 1.01 1.0) (list 1.01 0.99) 0.2)

#true

8 Testing

special form

(check-expect expression expected-expression)

Checks that the first expression produces the same value as the expected-expression. If the values are different, then check-expect reports it as a failed test case.

special form

(time expression)

Produces the same value as expression, but also prints the amount of time it took to calculate the value. The time is reported in milliseconds, measured in three ways (the distinctions between the three times are not important for this course).

9 Booleans

The Boolean type has two values: #true and #false.

There are a few traditional alternatives for referring to these two values. If you use only materials referred to in this instance of the course, then the alternatives should not appear, and you can safely ignore the rest of this paragraph. But if you look outside this instance of the course (for example, to some previous versions of the course), you might encounter the synonyms true or #t for #true, and false or #f for #false.

function

(not b)  boolean?

  b : boolean?
Negation of a boolean.
> (not #false)

#true

> (not #true)

#false

9.1 Conditional Expressions

These forms are special because they selectively (“conditionally”) compute the result value of some of their component expressions.

These forms also cannot be used as arguments, for example the following expressions each report an error:
> (apply and (list #true #true))

and: expects an open parenthesis before and, but found none

> (function? or)

or: expects an open parenthesis before or, but found none

special form

(and expression expression expression ...)

Do all the expressions produce the result value #true?

The result of each expression is computed, in order. This process ends early if either:
  • An expression produces #false, in which case #false is used immediately (“short-circuiting”) as the result of the whole and expression.

  • An expression produces a non-boolean value, in which case an error is reported.

special form

(or expression expression expression ...)

Does at least one of the expressions produce the result value #true?

The result of each expression is computed, in order. This process ends early if either:
  • An expression produces #true, in which case #true is used immediately (“short-circuiting”) as the result of the whole or expression.

  • An expression produces a non-boolean value, in which case an error is reported.

special form

(cond [question-expression answer-expression]
      ...
      [else answer-expression])
The result of the answer-expression corresponding to the first question-expression that produces #true or is else.

The paired question-and-answer [“condition” and “result”] expressions are known as “clauses”. People traditionally group a question-answer pair with square brackets, although parentheses can also be used, to emphasize that the clauses are not in the form of a function call, and to make it easier to see the two contained expression, which frequently are parenthesized forms themselves.

A cond computes each question-expression until: it finds the first one that produces #true, or all of them produce #false and it reaches the else. It then computes the result of the answer-expression for that clause, and uses that as the result of the whole cond expression.

If during this process one of the question-expressions produces a non-boolean value, then an error is reported.

9.2 Boolean Functions Documented in Another Section

function

(boolean? v)  boolean?

  v : any/c

function

(boolean=? b1 b2)  boolean?

  b1 : boolean?
  b2 : boolean?

10 Types of Data

The main types of data (“data-types”) in this course are:
  • image

  • number

  • function

  • string

  • boolean

  • list

10.1 Showing Data Types in DrRacket

To get used to the names of our main data-types and distinguishing which values have which type, you can make DrRacket show you the type of every result value in the Interactions, by following these steps:
  1. Select the DrRacket menu item “Language / Choose Language ...”.

  2. In the language chooser make sure that “CSC 104” is selected, and then click the “Show Details” button.

  3. Select the option “Prefix result values with their types”, then click the “Ok” button.

  4. The language shown in the lower left corner of DrRacket should now say “CSC 104 custom”, and be highlighted (confirming that a language option was changed).

  5. Click the “Run” button so that the change takes effect. The highlighting of “CSC 104 custom” should now go away, and the Interactions area should now say the language is “CSC 104 [custom]”.

    Enter a value into the Interactions area to see the new behaviour.

10.2 Type Predicates

Each of our main data-types has a corresponding predicate (a function producing a boolean) that determines whether a value has that type. The name of the predicate is the name of the data-type, suffixed with a “?”.

function

(image? v)  boolean?

  v : any/c
Is the argument an image?
> (image? image)

#true

function

(number? v)  boolean?

  v : any/c
Is the argument a number?
> (number? -1.04)

#true

> (number? "hello")

#false

See also Types of Numbers.

function

(string? v)  boolean?

  v : any/c
Is the argument a string?
> (string? "hello")

#true

> (string? 104)

#false

> (string? "")

#true

function

(boolean? v)  boolean?

  v : any/c
Is the argument a boolean?
> (boolean? #false)

#true

> (boolean? #true)

#true

> (boolean? 104)

#false

function

(list? v)  boolean?

  v : any/c
Is the argument a list?
> (list? (list 104 "hello world" (+ 1 2 3) (triangle 30 "solid" "blue")))

#true

> (list? "hello world")

#false

> (list? (+ 1 2 3))

#false

> (list? (list 1 2 3))

#true

> (list? (list))

#true

> (list? (list 104))

#true

See also empty? in Lists.

function

(function? v)  boolean?

  v : any/c
Is the argument a function?
> (function? circle)

#true

> (function? +)

#true

> (function? pi)

#false

10.3 Converting Between Types

function

(number->string x)  string?

  x : number?
A textual representation of the number.
> (number->string 104)

"104"

function

(string->number s)  (or/c number? #false)

  s : string?
The number corresponding to a textual representation of a literal number, or #false if the string is not in the form of a literal number.
> (string->number "-1.5")

-1.5

> (string->number "2/3")

2/3

> (string->number "(+ 2 3)")

#false

For functions to convert between a character and its Unicode number, see Unicode Numbering of Characters.

function

(string->image string font-size color)  image?

  string : string?
  font-size : (and/c natural? (between/c 1 255))
  color : image-color?
An image of the string, with a given font size and color.
> (string->image "Hi!" 30 "olive")

image

function

(string->list s)  (listof character?)

  s : string?
A list of the characters of the string.
> (string->list "a kitten")

(list "a" " " "k" "i" "t" "t" "e" "n")

11 Lists

A list bundles (“contains”, “holds”) zero or more values (known as its “members”, “elements”, or “items”), into a single “compound” value. One way to create a list is with list, which is also how lists are shown in the Interactions.

The list with no elements is known as the “empty” list. There are two traditional alternatives for referring to the empty list. If you use only materials referred to in this instance of the course, then the alternatives should not appear, and you can safely ignore the rest of this paragraph. But if you look outside this instance of the course (for example, to some previous versions of the course), you might encounter the synonyms empty or '().

function

(list v ...)  list?

  v : any/c
A list whose elements are the arguments.
> (list 104 "hello" #true)

(list 104 "hello" #true)

> (list "hello")

(list "hello")

> (list)

(list)

function

(length l)  natural?

  l : list?
The number of elements in a list.
> (length (list 104 "hello" #true))

3

function

(first l)  any/c

  l : list?
The first element of a (non-empty) list.
> (first (list 104 "hello" #true))

104

> (first (list))

first: expects a non-empty list, but received (list)

If first was not already in the language, it could be defined this way:
(define (first l)
  (list-ref l 0))

function

(rest l)  any/c

  l : list?
A version of the list, without the first element.
> (rest (list 104 "hello" #true))

(list "hello" #true)

> (rest (list))

rest: expects a non-empty list, but received (list)

function

(empty? v)  boolean?

  v : any/c
Is the value the empty list?
> (empty? (list))

#true

> (empty? (list 1 2 3))

#false

> (empty? 104)

#false

If empty? was not already in the language, it could be defined this way:
(define (empty? v)
  (equal? v (list)))

function

(second l)  any/c

  l : list?
(third l)  any/c
  l : list?
(fourth l)  any/c
  l : list?
The second, third, or fourth element of a list.
> (second (list 104 "hello" #true))

"hello"

> (third (list 104 "hello" #true))

#true

> (fourth (list 104 "hello" #true))

fourth: expects a list with four or more items, but received

(list 104 "hello" #true)

If second, third, and fourth were not already in the language, they could be defined with list-ref, for example:
(define (third l)
  (list-ref l 2))

function

(list-ref l i)  any/c

  l : list?
  i : natural?
The element in a list at the given index, counting from zero.
> (list-ref (list 104 "hello" #true) 0)

104

> (list-ref (list 104 "hello" #true) 1)

"hello"

> (list-ref (list 104 "hello" #true) 2)

#true

> (list-ref (list 104 "hello" #true) 3)

list-ref: expects a number less than the length of the list

for the second argument, but received 3

function

(reverse l)  list?

  l : list?
A version of the list, with the elements listed in reverse order (the last element first, the second-last element second, etc.).
> (reverse (list 104 "hello world" (+ 1 2 3) (triangle 30 "solid" "blue")))

(list image 6 "hello world" 104)

function

(list* v l)  list?

  v : any/c
  l : list?
A version of list l with v inserted at the front.
> (list* 104 (list "hello" #true))

(list 104 "hello" #true)

function

(append l ...)  list?

  l : list?
A single list containing the elements from all the lists (also known as “the concatenation” of all the lists).
> (append (list 1 2) (list "a" "b"))

(list 1 2 "a" "b")

> (append (list 1 2))

(list 1 2)

> (append)

(list)

function

(range start end step)  (listof number?)

  start : number?
  end : number?
  step : number?
The list of numbers from start to but not including end, changing by step.
> (range 2 10 3)

(list 2 5 8)

function

(map f l)  (listof Z)

  f : (A  Z)
  l : (listof A)
(map f l₁ l₂)  (listof Z)
  f : (A B  Z)
  l₁ : (listof A)
  l₂ : (listof B)
Performs a function on each value in a list, one at a time, separately, and gathers the resulting values into a list.
> (map string-length (list "rick" "and" "morty"))

(list 4 3 5)

> (list (string-length "rick") (string-length "and") (string-length "morty"))

(list 4 3 5)

The Intermediate Step we show in this course is:
(map f (list a b c ...))
 (list (f a) (f b) (f c) ...)
It can also be used with two lists of equal length, in which case it performs the function on pairs of elements from the two lists.
> (map +
       (list 1 2 3)
       (list 40 50 60))

(list 41 52 63)

> (list (+ 1 40) (+ 2 50) (+ 3 60))

(list 41 52 63)

The Intermediate Step we show in this course is:
(map f
     (list a1 ... an)
     (list b1 ... bn))
 (list (f a1 b1) ... (f an bn))

function

(apply f l)  B

  f : (A₁ ... Aₙ -> B)
  l : (list A₁ ... Aₙ)
The one result of calling f once, with the elements of l as all the arguments.
> (apply maximum (list 3 1 4 1 5 9 2))

9

> (maximum 3 1 4 1 5 9 2)

9

The Intermediate Step we show in this course is:
(apply f (list a1 ... an))
 (f a1 ... an)

function

(repeated f a n)  (listof A)

  f : (A -> A)
  a : A
  n : natural?
The list with elements a, (f a), (f (f a)), ..., of length n.
> (repeated sqr 2 3)

(list 2 4 16)

> (list 2 (sqr 2) (sqr (sqr 2)))

(list 2 4 16)

The Intermediate Step we show is:
(repeated f a n)
 (list a (f a) (f (f a)) ...)

function

(filter p? l)  (listof A)

  p? : (A -> boolean?)
  l : (listof A)
A version of the list l, with only the elements for which p? produces #true.
> (filter even? (list 3 1 4 1 5 9 2))

(list 4 2)

function

(sort l comparison)  (listof A)

  l : (listof A)
  comparison : (A A -> boolean?)
A version of the list l, with the elements ordered according to the predicate comparison.
> (sort (list 3 1 4 1 5 9 2 6) >)

(list 9 6 5 4 3 2 1 1)

> (sort (list "hello" "there" "friend") string<?)

(list "friend" "hello" "there")

function

(make-list n v)  list?

  n : natural?
  v : any/c
A list of n copies of v.
> (make-list 3 "hello")

(list "hello" "hello" "hello")

.

function

(member? v l)  boolean?

  v : any/c
  l : list?
Is v an element of the list l?
> (member? "hello" (list 2 "hello" #false))

#true

function

(remove v l)  list?

  v : any/c
  l : list?
A version of the list l, with the first occurrence of v removed, or just l if v is not an element of l.
> (remove "hello" (list 2 "hello" #true "hello"))

(list 2 #true "hello")

> (remove "bye" (list 2 "hello" #true "hello"))

(list 2 "hello" #true "hello")

function

(remove-all v l)  list?

  v : any/c
  l : list?
A version of the list l, with all occurrences of v removed.
> (remove-all "hello"  (list 2 "hello" #true "hello"))

(list 2 #true)

> (remove-all "bye" (list 2 "hello" #true "hello"))

(list 2 "hello" #true "hello")

function

(assoc v l)  (or/c list? #false)

  v : any/c
  l : (listof (list/c any/c any/c))
The first pair in the list of two-element lists l, whose first element is v, otherwise #false.
> (assoc "hello" (list (list "world" 2) (list "hello" 3) (list "good" 0)))

(list "hello" 3)

> (assoc "goodbye" (list (list "world" 2) (list "hello" 3) (list "good" 0)))

#false

11.1 List Functions Documented in Other Sections

function

(list? v)  boolean?

  v : any/c

12 Animation

special form

(big-bang start
          [on-tick updater]
          [to-draw drawer])
Launch a window with an animation.

The frames of the animation are the same as the images in the following list

(map drawer (repeated updater start n))

except big-bang does not actually create that list and goes on forever, not limited to generating some particular number of frames n.

For example,
> (define (rotated-star angle)
    (rotate (* 15 angle) (star 25 "solid" "blue")))
> (big-bang 0
            [on-tick add1]
            [to-draw rotated-star])
creates an animation from the following images:
> (map rotated-star (repeated add1 0 5))

(list image image image image image)

A big-bang expression also produes a result value, after you close its animation window: the updated version of start that was used to produce the last image shown.

special form

(big-bang start
          [on-tick updater])
The frames of the animation are the same as the images in the following list:

(repeated updater start n)

except big-bang does not actually create that list and goes on forever, not limited to generating some particular number of frames n. For example,
(big-bang (triangle 25 "solid" "blue")
          [on-tick rotate-cw])
creates an animation from the following images:
> (repeated rotate-cw (triangle 25 "solid" "blue") 5)

(list image image image image image)

special form

(big-bang start
          [to-draw drawer])
The frames of the animation are all the same: (drawer start).

special form

(big-bang start)

The frames of the animation are all the same: the start image.

13 Miscellaneous

function

(identity v)  any/c

  v : any/c
Just the argument, unchanged.
> (identity 104)

104

> (identity image)

image

function

(current-seconds)  natural?

The current time in seconds (counting from some platform-specific starting time).
> (current-seconds)

1532753127

14 Packages/Libraries

special form

(require library-name)

Make the definitions from a “library” (an installed package, or a special file) available to use in the current program.

If we use a library in the course, then we will describe how to install it (if necessary), and give you the exact expression to write in order to use the library.