For this assignment, you will modify and add to functions that are provided for you in hw6.scm. Download this file, rename it LASTNAME-hw6.scm
and enter your name in the comment at the top. Be careful to name your functions exactly as defined in the exercises, and be sure to comment each function as to its behavior.
Note that the first entry in the association list serves as a heading for the table. For this particular roster, the entry for each player contains (in order) their name (NAME), at-bats (AB), runs (R), hits (H), doubles (2B), triples (3B), home-runs (HR), runs-batted-in (RBI), walks (BB), hit-by-pitch (HBP) and sac-flies (SF). Note: different rosters might list the stats in a different order (as specified by the roster heading).
Add the following functions to your LASTNAME-hw6.scm
file. Note: you should comment
each function with a description of its behavior.
find-index
that takes two inputs, a stat abbreviation and a roster list, and returns the index of that stat entry in the roster. For example,
(find-index 'R CUBS)
should evaluate to 2
and (find-index 'SF CUBS)
should evaluate to 10
.
lookup
that takes three inputs, a player name, a stat abbreviation and a roster list, and returns the stat entry from the roster for that player. For example,
(lookup '(Nico Hoerner) 'R CUBS)
should evaluate to 60
and (lookup '(Ian Happ) 'SF CUBS)
should evaluate to 4
.
batting-avg
, on-base%
, total-bases
, slugging%
and OPS
. These new versions should each have only two inputs, the player name and a roster list, and should utilize the lookup
function to access the needed stats from the roster. For example, (total-bases '(Seiya Suzuki) CUBS)
should evaluate to 172
.
In class, we discussed how structured lists could be used to represent binary trees. For example, the following binary tree is represented by the list to the right:
|
Several utility functions (empty-tree?
, root
, left-subtree
,
and right-subtree
) are provided for you in hw6.scm
. Add the following functions for manipulating arbitrary binary trees.
rightmost
that takes one input, a list
representing a binary tree, and returns the rightmost value in the tree.
For example, (rightmost ANIMALS)
should
evaluate to possum
.
leftmost
that takes one input, a list
representing a binary tree, and returns the leftmost value in the tree.
For example, (leftmost ANIMALS)
should
evaluate to horse
.
height
that takes one input, a list
representing a binary tree, and returns the height of that tree. Recall that the height of a tree is the number of nodes on the longest path from the root to a leaf. For example, (height ANIMALS)
should evaluate to 3
.
Recall the following definitions from Data Structures:
|
Add the following functions for manipulating binary trees containing numbers:
is-bst?
that takes one input, a list of numbers
representing a binary tree, and returns #t
if that list
is a binary search tree (otherwise #f
). For example, (is-bst? NUMS)
should evaluate to #t
.
is-balanced?
that takes one input, a list of numbers
representing a binary tree, and returns #t
if that list
is relatively-balanced (otherwise #f
). For example, (is-balanced? NUMS)
should evaluate to #f
.
is-avl?
that takes one input, a list of numbers
representing a binary tree, and returns #t
if that list
is an AVL tree (otherwise #f
). For example, (is-avl? NUMS)
should evaluate to #f
.
In class, we discussed how first-class functions and closures could support the equivalent of classes, with private data and methods. The following function models a die object, where the number of sides is specified when creating the die.
For example:
count-roll
function from HW5 to utilize the above "class." Recall that this function takes two inputs, the desired dice total and the number of rolls. For example, (count-roll 7 1000)
should simulate 1,000 rolls of two 6-sided dice and return the number of 7's obtained. As before, this function must utilize tail-recursion. If we wanted to compare the likelihood of obtaining different dice totals, we could simply call the count-roll
function repeatedly, e.g., (count-rolls 2 10000)
, (count-rolls 3 10000)
, ..., (count-rolls 12 10000)
. However, it must be noted that each call to count-roll
is an independent experiment, and so the sum of the 11 counts would not necessarily add up to 10,000. To be more precise, we really need to conduct one sequence of rolls and keep count of all the totals at the same time.
zero-list
that takes one input, an integer, and returns a list containing that many zeros. For example, (zero-list 6)
should evaluate to (0 0 0 0 0 0)
. incr-index
that takes two inputs, an index and a list of numbers, and returns a copy of that list with the number at the specified index incremented. For example, (incr-index 0 '(0 0 0))
should evaluate to (1 0 0)
and (incr-index 2 '(1 0 2 1 4))
should evaluate to (1 0 3 1 4)
.count-all
that takes one input, a number of rolls. The function should simulate that many dice rolls and return a list of counts for each total. For example, (count-all 10000)
might return (303 576 869 1112 1386 1674 1297 1065 841 574 303)
, specifying that there were 303 twos, 576 threes, 869 fours, and so on. As was the case with count-roll
, your function must utilize tail-recursion.