Computational Thinking : CSC104 (Fall 2018)
This page is a reference for the University of Toronto “Computational Thinking” Teaching Language, used in the Fall 2018 offering of CSC 104.
5.2 Functions Operating on Functions, Documented in Another Section |
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 ...)
special form
(steps expression/definition ...)
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.
To copy an image from another application (for example a web browser), select/highlight it in the other application, using that application’s copy mechanism (which should be a menu item and/or a keyboard shortcut):
Then paste it into DrRacket (using DrRacket’s copy mechanism, for example the DrRacket menu item “Edit / Paste”, or the associated keyboard shortcut):
Sometimes, the copy-paste approach does not work for a particular application. If you can save the image from the application to a file on your computer, then you can insert it into DrRacket using the menu item “Insert / Insert Image...” and choosing the file:
If the image is on the web, and you know its address (“URL”), you can use url->image to download and refer to it. First copy the URL, for example from your browser address bar:
Then use the following function (be sure to use the double-quotes, to make it a “text”):
function
(url->image url) → image?
url : text?
alias
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
> (beside ![]()
![]()
)
> (above ![]()
![]()
)
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?
> (align-beside "top" ![]()
![]()
)
> (align-above "left" ![]()
![]()
)
> (align-beside "baseline" (text->image "play" 30 "tomato") (text->image "a" 40 "blue") (text->image "jig" 20 "darkgreen"))
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?
4.3 Rotating, Scaling, Mirroring, Flipping, and Cutting
function
image : image? angle : number? (counter image) → image? image : image? (clockwise image) → image? image : image?
> (define a-half-triangle (cut-right (triangle 40 "solid" "forestgreen") 20)) > a-half-triangle
> (counter a-half-triangle)
> (clockwise a-half-triangle)
function
image : image? factor : (and/c number? non-negative?) (shrink image) → image? image : image? (enlarge image) → image? image : image?
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?)
4.4 Image Properties
> (width (rectangle 5 30 "solid" "purple")) 5
> (height (rectangle 5 30 "solid" "purple")) 30
> (width (circle 200 "solid" "orange")) 200
> (height (above (circle 200 "solid" "orange") (rectangle 5 30 "solid" "purple"))) 230
4.5 Geometric Shapes
function
radius : (and/c number? non-negative?) shading : (or/c "solid" "outline") color : color?
function
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?
function
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?
> (rectangle 40 20 "outline" "black")
> (rectangle 20 40 "solid" "aquamarine")
> (ellipse 40 20 "outline" "maroon")
> (ellipse 20 40 "solid" "red")
function
distance : (and/c number? non-negative?) shading : (or/c "solid" "outline") color : color?
> (star 20 "solid" "yellow")
4.5.1 Color
> (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
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
(image->colors image) → colors?
image : image?
alias
alias
alias
function
(colors->image colors width height) → image?
colors : colors? width : natural? height : natural?
alias
alias
alias
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?
5 Functions
5.1 Types of Functions
> (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
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?
6 Numbers
6.1 Arithmetic
> (+) 0
> (*) 1
> (- 7) -7
> (- 7 2) 5
> (/ 12 2) 6
> (squared 5) 25
function
(square-root x) → number?
x : (and/c number? non-negative?)
> (square-root 9) 3
> (square-root 2) #i1.4142135623730951
> 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”.
function
m : natural? n : (and/c natural? positive?) (remainder m n) → natural? m : natural? n : (and/c natural? positive?)
6.2 Rounding
6.3 Comparing Numbers
6.4 Types of Numbers
(natural? v): is v a natural number (one of the numbers 0, 1, 2, 3, ...)?
(define (natural? v) (and (integer? v) (non-negative? v)))
function
x : number? (zero? x) → boolean? x : number? (non-negative? x) → boolean? x : number? (positive? x) → boolean? x : number?
(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?
> (non-negative? -2) #false
> (non-negative? 0) #true
> (non-negative? 2) #true
(define (non-negative? x) (>= x 0))
(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
6.6 Number Functions Documented in Other Sections
function
(number->text x) → text?
x : number? (text->number t) → (or/c number? #false) t : text?
7 Definitions
special form
(define name 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)
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)
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?
> (text-length "hello world") 11
> (text-join "hello" " " "world" ", " "good bye") "hello world, good bye"
> (text-join "hello") "hello"
> (text-join) ""
> (sub-text? "it" "kitten") #true
function
(character? v) → boolean?
v : any/c
> (character? 1) #false
> (character? "a") #true
> (character? "abc") #false
function
(character t i) → character?
t : text? i : natural?
> (character "hello world" 1) "e"
8.1 Types of Characters
function
(upper-case? t) → boolean?
t : text? (lower-case? t) → boolean? t : text?
> (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?
> (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?
> (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?
> (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
function
(character->unicode s) → natural?
s : character?
alias
function
(unicode->character n) → character?
n : natural?
alias
> (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.
10 Testing
special form
(check-expect expression expected-expression)
special form
(time expression)
11 Booleans
11.1 Conditional Expressions
These forms are special because they selectively (“conditionally”) compute the value of some of their component expressions.
> (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 ...)
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 ...)
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 paired condition-and-result expressions are known as “if clauses”.
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.
11.2 Boolean Functions Documented in Another Section
12 Types of Data
image
number
function
text
boolean
list
12.1 Showing Data Types in DrRacket
Select the DrRacket menu item “Language / Choose Language ...”.
In the language chooser make sure that “CSC 104” is selected, and then click the “Show Details” button.
Select the option “Prefix result values with their types”, then click the “Ok” button.
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).
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
> (image? )
#true
> (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
12.3 Converting Between Types
function
(number->text x) → text?
x : number?
alias
> (number->text 104) "104"
function
(text->number t) → (or/c number? #false)
t : text?
alias
> (text->number "-1.5") -1.5
> (text->number "2/3") 2/3
> (text->number "(+ 2 3)") #false
function
(text->image text font-size color) → image?
text : text? font-size : (and/c number? (between/c 1 255)) color : color?
alias
> (text->image "Hi!" 30 "olive")
function
(text->list t) → (listof character?)
t : text?
alias
> (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.
> (first (list 104 "hello" #true)) 104
> (first (list)) first : expects a non-empty list for the first argument, but
received (list)
> (rest (list 104 "hello" #true)) (list "hello" #true)
> (rest (list)) rest : expects a non-empty list for the first argument, but
received (list)
> (empty? (list)) #true
> (empty? (list 1 2 3)) #false
> (empty? 104) empty? : expects a list for the first argument, but received
104
> (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)
> (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
> (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)
> (range 2 10 3) (list 2 5 8)
> (reverse (list 104 "hello world" (+ 1 2 3) (triangle 30 "solid" "blue"))) (list
6 "hello world" 104)
> (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)
> (map + (list 1 2 3) (list 40 50 60)) (list 41 52 63)
> (list (+ 1 40) (+ 2 50) (+ 3 60)) (list 41 52 63)
> (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")
> (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])
> (define (rotated-star angle) (rotate (star 25 "solid" "blue") angle))
> (big-bang 0 [on-tick inc] [to-draw rotated-star]) function call: expected at least one argument expression
after void, but found none
special form
(big-bang start [on-tick updater])
(repeats updater start n)
special form
(big-bang start [to-draw drawer])
special form
(big-bang start)
15 Miscellaneous
function
(identity v) → any/c
v : any/c
function
(url->browser url) → void?
url : text?
alias
function
(current-seconds) → natural?
16 Packages/Libraries
special form
(require library-name)
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.