Grabbing a Single Row
In addition to Numbers, Strings, Booleans, Images and Tables, Pyret has a data type for an individual Row
.
Open the Animals Starter File and click "Run". In the Interactions Area (right), type animals-table
. Hit "Enter" to see the default view of the table.
Then type row-n(animals-table, 2)
and compare the result to the table.
1 Write the code that generates the first row of the table.
2 Explain what the second input to row-n
means, in as much detail as possible.
Grabbing Multiple Rows
3 Type first-n-rows(animals-table, 5)
. What happens?
4 If we wanted a table of the first 3 rows of the animals-table
, what code would we write?
5 What is the Contract for first-n-rows
?
Defining Values
Pyret lets us define values that we want to use later. We can define any kind of values we like!
6 If we tell Pyret that x = 4 * 2
, what would you expect to get back when you type x + 1
?
Test it out by typing x = 4 * 2
into the Interactions Area, hitting "Enter" and then typing x + 1
.
7 Try typing gt = triangle(50, "solid", "green")
and hitting "Enter".
What happens?
Now type gt
. What do you get back?
8 Explain what is happening on Line 14 of the Animals Starter File.
9 On line 16 of the Definitions Area, add a new definition called my-pet
, which is defined to be your favorite animal.
code:
10 Add a new line at the bottom of the Definitions Area, define first-3
to be a subset of the first 3 rows of the animals-table.
code:
★ What happens when you type first-n-rows(sort(animals-table, "pounds", true), 5)
?
Note: In this case, the output of sort(animals-table, "pounds", true)
is the Table first-n-rows
is taking in!
★ ★ See if you can figure out how to compose the code that would generate a table of the 10 oldest animals!
function-name (Table, Number)
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.