CSCI3136
Assignment 10
Due: 9:00am, Monday, July 27, 2020
1. [5 marks] In one of the examples for Lecture 20, we saw how to implement a closure that
can be used with the list-visitor function to compute the average of a list. Using the
example as a starting point, implement a function called new-range, which returns a closure
that can be used with the list-visitor function to compute the range of a list of numbers.
Recall that the range of a list of numbers is defined as
range(L) = max (L) − min (L)
Write the new-range function in a file called range-visitor.scm.
2. [5 marks] In Java we can instantiate an iterator of a list. Each time the iterator is called,
it returns the next item in a list. Implement a function called new-abs-iterator that takes
a list of numbers as an argument and returns an iterator (implemented using a closure) that
returns the absolute value of the next item in the list when it is called. Once the list is empty,
the boolean #f should be returned. For example
> (define abx (new-abs-iterator ’(-1 2 -3 4)))
Write the new-abs-iterator function in a file called abs-iterator.scm.
Note: Scheme has an abs function that returns the absolute value of a number.
3. [5 marks] Using your code from Question 2 as a starting point, Implement a function called
new-positive-iterator that takes a list of numbers as an argument and returns an iterator
(implemented using a closure) that returns the next positive item in the list when it is called.
Once the list is empty, the boolean #f should be returned. For example
1
CSCI3136 Summer 2020 Assignment 10
> (define pos (new-positive-iterator ’(-1 2 -3 4)))
Write the new-positive-iterator function in a file called positive-iterator.scm.
4. [5 marks] Rewrite the following code as a tail-recursive function, called max-tr. Write the
max-tr function in a file called max.scm.
(define max-nt (lambda (L)
(if (null? (cdr L))
(car L)
(max (car L) (max-nt (cdr L)))
)
)
)
5. [Bonus 5 marks] Using the code in bst.scm as a starting point implement a function called
new-bst-iterator that takes a BST as an argument and returns an iterator (implemented
using a closure) that returns the next item in an in-order traversal of the BST when it is
called. Once the traversal is complete, the boolean #f should be returned.
Write the new-bst-iterator function in the file bst.scm.
Marking Scheme
1. Marking scheme for Questions 1, 2, 3, 4, and 5 (Bonus).
3 points 2 points 1 point 0 points
Solution Solution looks correct
and covers all cases
Solution looks mostly
correct and covers
some cases
Solution represents a
good attempt No solution provided
Functionality Solution works on test
input
Solution works on
some test inputs
Solution does not
work
2. Marking scheme for overall code clarity.
2 points 1 point 0 points
Understandability Code is easy to understand Code is a little hard to understand
Code is unintelligible or not
submitted
Formatting Code is well formatted and
indented
Code is inconsistently formatted
Code is poorly formatted or
not submitted
appropriate
code submitted
2
CSCI3136: Assignment 10
Summer 2020
Student Name Login ID Student Number Student Signature
Mark
Question 1 /5
Question 2 /5
Question 3 /5
Question 4 /5
Bonus Question 4 /5
Overall code clarity /5
Total /25
Assignments are due by 9:00am on the due date. Assignments must be submitted electronically