# update-danger :: Number -> Number
# takes in danger’s x-coordinate and produces the next one
This function takes in the danger’s x-coordinate and produces the next one, but it doesn’t do anything with the y-coordinate. That’s why our DANGER
can only move left or right! Suppose we wanted to change the function, so it updates the character by moving diagonally…
1 Stop and think! What, if anything, will have to change about the Domain?
2 To move diagonally, we need the function to be aware of both the x and the y-coordinate. It will have to consume both x and y, so the Domain will change to Number Number
. What other functions have you used that consume two Numbers?
3 Stop and think! What, if anything, will have to change about the Range?
The Range can’t just be one Number, since an (x, y) coordinate has two Numbers. One idea might be to write the contract this way:
# update-danger :: Number Number -> Number Number
# take in danger’s x- and y-coordinate, and produces the next x- and next y-coordinate
Unfortunately, that contract breaks an important rule about functions: Given an input, all functions must produce one output!
Fortunately, our programming language has another data type, called a Posn
. A Posn
- short for "position" - is a piece of data that contains two Numbers! We can make a posn
to represent the position (100, 200) with the following code:
make-posn(100, 200)
4 On the computer, try making a Posn
or two. What expression will make a Posn
representing the origin?
5 Write the contract for the make-posn
function on the line below.
6 On the lines below, write the new contract and purpose for update-danger
, so that it produces a Posn
instead of a Number.
7 In your game file, change your contract, purpose, examples, and definition to make your DANGER
and TARGET
move in two dimensions!
These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, and 1738598). 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.