Computational Thinking :   CSC104 (Fall 2018)
1 Acknowledgements
2 The Definitions and Interactions Areas
2.1 Inserting Comments
3 Seeing the Steps to Compute an Expression
step
steps
4 Images
4.1 Literal Images
4.1.1 Copying Images in Dr  Racket
url->image
url→image
4.2 Combining Images
beside
above
overlay
align-beside
align-above
align-overlay
4.3 Rotating, Scaling, Mirroring, Flipping, and Cutting
rotate
counter
clockwise
scale
shrink
enlarge
mirror
flip
cut-bottom
cut-top
cut-right
cut-left
4.4 Image Properties
width
height
4.5 Geometric Shapes
circle
square
triangle
rectangle
ellipse
star
4.5.1 Color
color?
colour?
4.5.2 Pixels
render
image->colors
image->colours
image→colors
image→colours
colors->image
colours->image
colors→image
colours→image
colors?
colours?
4.6 Image Functions Documented in Other Sections
5 Functions
5.1 Types of Functions
unary?
binary?
5.2 Functions Operating on Functions, Documented in Another Section
6 Numbers
6.1 Arithmetic
+
*
-
/
inc
dec
squared
square-root
power
pi
π
6.1.1 Whole-Number Division
quotient
remainder
6.2 Rounding
round
floor
ceiling
6.3 Comparing Numbers
=
<
>
<=
>=
maximum
minimum
6.4 Types of Numbers
integer?
natural?
negative?
zero?
non-negative?
positive?
even?
odd?
6.5 Generating Random Numbers
random
6.6 Number Functions Documented in Other Sections
7 Definitions
define
7.1 Temporary Definitions
local
8 Texts
text-length
text-join
sub-text
sub-text?
character?
character
8.1 Types of Characters
letters?
digits?
upper-case?
lower-case?
whitespace?
8.2 Case Conversion
upper-case
lower-case
8.3 Alphabetic Order
alphabet-order?
8.4 Unicode Numbering of Characters
character->unicode
character→unicode
unicode->character
unicode→character
9 Comparison
same?
differ?
10 Testing
check-expect
time
11 Booleans
not
11.1 Conditional Expressions
and
or
if
else
11.2 Boolean Functions Documented in Another Section
12 Types of Data
12.1 Showing Data Types in Dr  Racket
12.2 Type Predicates
image?
number?
text?
boolean?
list?
function?
12.3 Converting Between Types
number->text
number→text
text->number
text→number
text->image
text→image
text->list
text→list
13 Lists
list
length
first
rest
empty?
second
third
fourth
element
join
adjoin
sub-list
range
reverse
map
apply
repeats
sift
sort
element?
remove
13.1 List Functions Documented in Other Sections
14 Animation
big-bang
15 Miscellaneous
identity
url->browser
url→browser
current-seconds
16 Packages/  Libraries
require
7.1

Computational Thinking : CSC104 (Fall 2018)

Gary Baumgartner <[email protected]>

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

    1 Acknowledgements

    2 The Definitions and Interactions Areas

      2.1 Inserting Comments

    3 Seeing the Steps to Compute an Expression

    4 Images

      4.1 Literal Images

        4.1.1 Copying Images in DrRacket

      4.2 Combining Images

      4.3 Rotating, Scaling, Mirroring, Flipping, and Cutting

      4.4 Image Properties

      4.5 Geometric Shapes

        4.5.1 Color

        4.5.2 Pixels

      4.6 Image Functions Documented in Other Sections

    5 Functions

      5.1 Types of Functions

      5.2 Functions Operating on Functions, Documented in Another Section

    6 Numbers

      6.1 Arithmetic

        6.1.1 Whole-Number Division

      6.2 Rounding

      6.3 Comparing Numbers

      6.4 Types of Numbers

      6.5 Generating Random Numbers

      6.6 Number Functions Documented in Other Sections

    7 Definitions

      7.1 Temporary Definitions

    8 Texts

      8.1 Types of Characters

      8.2 Case Conversion

      8.3 Alphabetic Order

      8.4 Unicode Numbering of Characters

    9 Comparison

    10 Testing

    11 Booleans

      11.1 Conditional Expressions

      11.2 Boolean Functions Documented in Another Section

    12 Types of Data

      12.1 Showing Data Types in DrRacket

      12.2 Type Predicates

      12.3 Converting Between Types

    13 Lists

      13.1 List Functions Documented in Other Sections

    14 Animation

    15 Miscellaneous

    16 Packages/Libraries

1 Acknowledgements

Aliya Hameer implemented a significant portion of the 2017 Fall version of the language, which this 2018 Fall version is based on.

2 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.

2.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 the color of the definition or expression doesn’t change.

3 Seeing the Steps to Compute an Expression

special form

(step expression/definition
      ...)
Print the steps of computation, waiting for the user to press Enter/Return between steps.

special form

(steps expression/definition
       ...)
Print the steps of computation without waiting. Limitations: these can’t step into big-bang, require, and time expressions.

4 Images

4.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.

4.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

(url->image url)  image?

  url : text?

alias

url→image : url->image

The image at the web address url.

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

4.2 Combining Images

function

(beside image ...)  image?

  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 image ...)  image?

  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 image ...)  image?

  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

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

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

image

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

image

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

image

function

(align-overlay horizontal-alignment    
  vertical-alignment    
  image ...)  image?
  horizontal-alignment : (or/c "left" "right" "center")
  vertical-alignment : (or/c "top" "bottom" "center" "baseline")
  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.

4.3 Rotating, Scaling, Mirroring, Flipping, and Cutting

function

(rotate image angle)  image?

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

image

> (rotate image -10)

image

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

image

> (counter a-half-triangle)

image

> (clockwise a-half-triangle)

image

function

(scale image factor)  image?

  image : image?
  factor : (and/c number? non-negative?)
(shrink image)  image?
  image : image?
(enlarge image)  image?
  image : image?
The image, scaled in both directions by the amount factor, 1/2, or 2.
> (scale image 3)

image

> (shrink image)

image

> (enlarge image)

image

function

(mirror image)  image?

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

image

> (above image (flip image))

image

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

function

(cut-bottom image height)  image?

  image : image?
  height : (and/c number? non-negative?)
(cut-top image height)  image?
  image : image?
  height : (and/c number? non-negative?)
(cut-right image width)  image?
  image : image?
  width : (and/c number? non-negative?)
(cut-left image width)  image?
  image : image?
  width : (and/c number? non-negative?)
A version of the image, with the specified height or width cut off 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.
> (cut-bottom image 9)

image

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

image

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

image

4.4 Image Properties

function

(width image)  natural?

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

5

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

30

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

400

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

430

4.5 Geometric Shapes

function

(circle radius shading color)  image?

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

image

> (circle 10 "solid" "blue")

image

function

(square side-length shading color)  image?

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

image

> (square 30 "outline" "brown")

image

> (triangle 30 "solid" "darkgreen")

image

function

(rectangle width height shading color)  image?

  width : (and/c number? non-negative?)
  height : (and/c number? non-negative?)
  shading : (or/c "solid" "outline")
  color : color?
(ellipse width height shading color)  image?
  width : (and/c number? non-negative?)
  height : (and/c number? non-negative?)
  shading : (or/c "solid" "outline")
  color : color?
A rectangle, or an ellipse, with the given width, height, shading, 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 shading color)  image?

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

image

4.5.1 Color

function

(color? v)  boolean?

  v : any/c

alias

colour? : color?

Is the argument a text containing the name of a color, or a list of three or four percentages (numbers between 0 and 100 inclusive).
> (color? (list 12 56 34))

#true

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

#true

> (color? "orange")

#true

> (color? "ORANGE")

#true

> (color? "Black")

#true

> (color? "bloo")

#false

> (color? "transparent")

#true

The list of recognized color names is shown in color-database<%>, except "transparent" is also considered a color. Color naming is case-insensitive.

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

4.5.2 Pixels

function

(render image)  image?

  image : image?
The image, but without the hidden information about how it was constructed. Using this can improve the drawing speed, but loses information that which might end up making the image look worse if it is rotated or scaled up (if the original was scaled down).
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 color? for the meaning of the percentages).

function

(colors->image colors width height)  image?

  colors : colors?
  width : natural?
  height : natural?

alias

colours->image : colors->image

alias

colors→image : colors->image

alias

colours→image : colors->image

An image built from the list of colors, each color represented by a list of four percentages, (see 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

alias

colours? : colors?

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

4.6 Image Functions Documented in Other Sections

function

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

  text : text?
  font-size : (and/c number? (between/c 1 255))
  color : color?

function

(image? v)  boolean?

  v : any/c

5 Functions

5.1 Types of Functions

function

(unary? f)  boolean?

  f : function?
(binary? f)  boolean?
  f : function?
Can the function be called with one, or two, arguments, respectively?
> (unary? flip)

#true

> (unary? rotate)

#false

> (unary? +)

#true

> (binary? flip)

#false

> (binary? rotate)

#true

> (binary? +)

#true

5.2 Functions Operating on Functions, Documented in Another Section

function

(function? v)  boolean?

  v : any/c

function

(map f l)  list?

  f : unary?
  l : list?
(apply f l)  any/c
  f : function?
  l : list?
(repeats f seed n)  list?
  f : unary?
  seed : any/c
  n : natural?
(sift p? l)  list?
  p? : unary?
  l : list?
(map f l₁ l₂)  list?
  f : binary?
  l₁ : list?
  l₂ : list?
See Lists.

6 Numbers

6.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

(inc x)  number?

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

4

> (dec 3)

2

Incrementing (adding 1) and decrementing (subtracting 1) are somewhat common in programming. Using dedicated 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. They’re also simple unary functions for us to use in examples, so we’ve abbreviated their names to make typing out examples about easy as if using (+ x 1) or (- x 1).

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

function

(squared x)  number?

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

25

If squared was not already in the language, it could be defined this way:
(define (squared x)
  (* x x))

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?

alias

π : pi

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

#i3.141592653589793

6.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 general 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)))

6.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

6.3 Comparing 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 the same?
> (= 2 3)

#false

> (= 3 3)

#true

With more than two arguments: are all the numbers the same?
> (= 3 3 2 3)

#false

> (= 3 3 3 3 3)

#true

See also Comparison for a discussion of same?, 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?

alias

 : <=

function

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

  x : number?
  y : number?
  z : number?

alias

 : >=

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

6.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 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 same? in Comparison.

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.

6.5 Generating Random Numbers

function

(random n)  natural?

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

6.6 Number Functions Documented in Other Sections

function

(number? v)  boolean?

  v : any/c

function

(number->text x)  text?

  x : number?
(text->number t)  (or/c number? #false)
  t : text?

7 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 substituted 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.

7.1 Temporary Definitions

special form

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

Each definition must be a define form.

Each definition occurs, and then the value 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.

8 Texts

A text 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 text 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 text as one of its characters requires some way to indicate that the double-quote is not just signalling the end of the text. 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 texts will contain double-quotes as characters, nor span multiple lines.

function

(text-length t)  natural?

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

11

function

(text-join t ...)  text?

  t : text?
A single text formed by joining the given texts.
> (text-join "hello" " " "world" ", " "good bye")

"hello world, good bye"

> (text-join "hello")

"hello"

> (text-join)

""

function

(sub-text t i j)  text?

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

"ello w"

> (sub-text "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 text t, otherwise an error is reported.

function

(sub-text? t u)  boolean?

  t : text?
  u : text?
Does the first text appear inside the second one?
> (sub-text? "it" "kitten")

#true

function

(character? v)  boolean?

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

#false

> (character? "a")

#true

> (character? "abc")

#false

function

(character t i)  character?

  t : text?
  i : natural?
The character in the text at the given index (counting from zero).
> (character "hello world" 1)

"e"

If character was not in the language, it could be defined this way:
(define (character s i)
  (sub-text s i (inc i)))

8.1 Types of Characters

function

(letters? t)  boolean?

  t : text?
Does the text contain only letters?
> (letters? "Cat")

#true

> (letters? "Cat?")

#false

> (letters? "123")

#false

function

(digits? t)  boolean?

  t : text?
Does the text contain only digits?
> (digits? "123")

#true

> (digits? "-23")

#false

function

(upper-case? t)  boolean?

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

#true

> (upper-case? "kITTY")

#false

> (upper-case? "CAT?")

#false

> (lower-case? "cat")

#true

> (upper-case? "kITTY")

#false

> (lower-case? "cat?")

#false

function

(whitespace? t)  boolean?

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

#true

> (whitespace? " a ")

#false

> (whitespace? "\n")

#true

8.2 Case Conversion

function

(upper-case t)  text?

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

"KITTY!"

> (lower-case "kIttY!")

"kitty!"

8.3 Alphabetic Order

function

(alphabet-order? t u v ...)  boolean?

  t : text?
  u : text?
  v : text?
Are the texts in alphabetic order?

Upper-case letters are considered to be alphabetically before lower-case letters (for historical reasons).
> (alphabet-order? "al" "beth" "dee")

#true

> (alphabet-order? "Beth" "al" "beth" "dee")

#true

> (alphabet-order? "zebra" "koala" "kitten")

#false

> (alphabet-order? "kitten" "koala" "zebra")

#true

8.4 Unicode Numbering of Characters

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)

"⚘"

9 Comparison

The functions same?, and differ?, determine whether two values are the same, or different.

See = for the version of same? restricted to numbers, which can also be used with more than two numbers.

function

(same? v₁ v₂)  boolean?

  v₁ : any/c
  v₂ : any/c
(differ? v₁ v₂)  boolean?
  v₁ : any/c
  v₂ : any/c
> (same? (list 1 2) (list (dec 2) (+ 1 1)))

#true

> (differ? (list 1 2) (list (dec 2) (+ 1 1)))

#false

10 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).

11 Booleans

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

function

(not b)  boolean?

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

#true

> (not #true)

#false

11.1 Conditional Expressions

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

Since these forms need to take control, they cannot be used as arguments. For example, the following expressions each report an error:
> (function? or)

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

> (apply if #true 1 2)

if: expected an open parenthesis before if, but found none

> (map and
       (list #true #true)
       (list #false #true))

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

special form

(and expression expression expression ...)

Do all of the expressions produce the value #true?

The value 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 ...)

Do at least one of the expressions produce the value #true?

The value 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

(if [condition-expression result-expression]
    [condition-expression result-expression]
    ...
    [else result-expression])
The value of the result-expression corresponding to the first condition-expression that produces #true or is else.

The paired condition-and-result expressions are known as “if clauses”.

People traditionally group a condition-result pair with square brackets (although parentheses can also be used) to:
  • Emphasize that the pairing doesn’t mean the usual function call.

  • Make it easier to see the two contained expressions, which are themselves often parenthesized forms.

The value of each condition-expression is computed, in order, until either:
  • A condition produces #true, or they were all #false and the else was reached, in which case the value of the result-expression for that clause is computed and its value is used as the result of the whole if expression.

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

11.2 Boolean Functions Documented in Another Section

function

(boolean? v)  boolean?

  v : any/c

12 Types of Data

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

  • number

  • function

  • text

  • boolean

  • list

12.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.

12.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

(text? v)  boolean?

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

#true

> (text? 104)

#false

> (text? "")

#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

12.3 Converting Between Types

function

(number->text x)  text?

  x : number?

alias

number→text : number->text

A text representation of the number.
> (number->text 104)

"104"

function

(text->number t)  (or/c number? #false)

  t : text?

alias

text→number : text->number

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

-1.5

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

2/3

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

#false

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

function

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

  text : text?
  font-size : (and/c number? (between/c 1 255))
  color : color?

alias

text→image : text->image

An image of the text, with a given font size and color.
> (text->image "Hi!" 30 "olive")

image

function

(text->list t)  (listof character?)

  t : text?

alias

text→list : text->list

A list of the characters of the text.
> (text->list "a kitten")

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

13 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.

function

(list e ...)  list?

  e : 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 for the first argument, but

received (list)

If first was not already in the language, it could be defined this way:
(define (first l)
  (element 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 for the first argument, 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)

empty? : expects a list for the first argument, but received

104

If empty? was not already in the language, it could be defined this way:
(define (empty? v)
  (same? 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 at least four elements for the

first argument, but received (list 104 "hello" #true)

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

function

(element l i)  any/c

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

104

> (element (list 104 "hello" #true) 1)

"hello"

> (element (list 104 "hello" #true) 2)

#true

> (element (list 104 "hello" #true) 3)

element : expects a number less than the length of the list

for the second argument, but received 3

function

(join l ...)  list?

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

(list 1 2 "a" "b")

> (join (list 1 2))

(list 1 2)

> (join)

(list)

function

(adjoin e l)  list?

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

(list 104 "hello" #true)

function

(sub-list l i j)  list?

  l : list?
  i : natural?
  j : natural?
The elements of the list l starting from the element at index i (counting from zero), up to but not including the element at index j.
> (sub-list (list "hello" 123 (list 4 5 6) #false) 1 3)

(list 123 (list 4 5 6))

> (sub-list (list "hello" 123 (list 4 5 6) #false) 2 2)

(list)

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 list l, otherwise an error is reported.

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

(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

(map f l)  list?

  f : unary?
  l : list?
(map f l₁ l₂)  list?
  f : binary?
  l₁ : list?
  l₂ : list?
Performs a function on each value in a list, one at a time, separately, and gathers the resulting values into a list.
> (map text-length (list "rick" "and" "morty"))

(list 4 3 5)

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

(list 4 3 5)

The algebraic step for this is:
(map f (list a b c  ...))
 (list (f a) (f b) (f c) ...)
It can also be used with two lists of the same 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 algebraic step for this is:
(map f
     (list a₁ b₁ c₁ ...)
     (list a₂ b₂ c₂ ...))
 (list (f a₁ a₂) (f b₁ b₂) (f c₁ c₂) ...)

function

(apply f l)  any/c

  f : function?
  l : list?
The one result of calling f where the arguments have been grouped into a list.
> (apply maximum (list 3 1 4 1 5 9 2))

9

> (maximum 3 1 4 1 5 9 2)

9

The algebraic step for this is:
(apply f (list a b c ...))
 (f a b c ...)

function

(repeats f seed n)  list?

  f : unary?
  seed : any/c
  n : natural?
The list with elements a, (f a), (f (f a)), ..., of length n.
> (repeats squared 2 3)

(list 2 4 16)

> (list 2 (squared 2) (squared (squared 2)))

(list 2 4 16)

The algebraic step for this is:
(repeats f a n)
 (list a (f a) (f (f a)) ...)

function

(sift p? l)  list?

  p? : unary?
  l : list?
A version of the list l, keeping only the elements for which p? produces #true.
> (sift even? (list 3 1 4 1 5 9 2))

(list 4 2)

function

(sort comparison l)  list?

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

(list 9 6 5 4 3 2 1 1)

> (sort alphabet-order? (list "hello" "there" "friend"))

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

function

(element? e l)  boolean?

  e : any/c
  l : list?

alias

 : element?

Is e an element of the list l?
> (element? "hello" (list 2 "hello" #false))

#true

function

(remove e l)  list?

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

(list 2 #true)

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

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

13.1 List Functions Documented in Other Sections

14 Animation

special form

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

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

(map drawer (repeats updater start n))

except big-bang does not actually create that list, but goes on forever, not limited to generating some particular number of frames n. For example,
> (define (rotated-star angle)
    (rotate (star 25 "solid" "blue") angle))
> (big-bang 0
    [on-tick inc]
    [to-draw rotated-star])
creates an animation from the following images:
> (map rotated-star (repeats inc 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 animation frames are the same as the images in the following list:

(repeats updater start n)

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

(list image image image image image)

special form

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

special form

(big-bang start)

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

15 Miscellaneous

function

(identity v)  any/c

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

104

> (identity image)

image

function

(url->browser url)  void?

  url : text?

alias

url→browser : url->browser

Open the web page at the url address, in your default browser.

function

(current-seconds)  natural?

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

16 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.