Open WeScheme and click “Run”. We will be working in the Interactions Area on the right.
Test out these two expressions and record what you learn below:
-
(regular-polygon 40 6 "solid" "green")
-
(regular-polygon 80 5 "outline" "dark-green")
1 You’ve seen data types like Numbers, Strings, and Booleans. What data type did the regular-polygon
function produce?
2 How would you describe what a regular polygon is?
3 The regular-polygon
function takes in four pieces of information (called arguments). Record what you know about them below.
Data Type | Information it Contains | |
---|---|---|
Argument 1 |
||
Argument 2 |
||
Argument 3 |
||
Argument 4 |
There are many other functions available to us in Pyret. We can describe them using contracts. The Contract for regular-polygon
is:
; regular-polygon :: Number, Number, String, String -> Image
-
Each Contract begins with the function name: in this case
regular-polygon
-
Lists the data types required to satisfy its Domain: in this case Number, Number, String, String
-
And then declares the data type of the Range it will return: in this case Image
Contracts can also be written with more detail, by annotating the Domain with variable names:
; regular-polygon :: (Numbersize, Numbernumber-of-sides, Stringfill-style, Stringcolor) -> Image
4 We know that a square is a regular polygon because
5 What code would you write to make a big, blue square using the regular-polygon
function?
function-name ( size :: Number,
number-of-sides :: Number,
fill-style :: String,
color :: String)
6 Pyret also has a square
function whose contract is: ; square :: (Numbersize, Stringfill-style, Stringcolor) -> Image
What code would you write to make a big blue square using the square
function?
function-name ( size :: Number,
fill-style :: String,
color :: String)
7 Why does square
need fewer arguments to make a square than regular-polygon
?
★ Where else have you heard the word contract used before?
These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, 1738598, 2031479, and 1501927). Bootstrap by the Bootstrap Community is licensed under a Creative Commons 4.0 Unported License. This license does not grant permission to run training or professional development. Offering training or professional development with materials substantially derived from Bootstrap must be approved in writing by a Bootstrap Director. Permissions beyond the scope of this license, such as to run training, may be available by contacting contact@BootstrapWorld.org.