CSci 1301: Lab 6

Due: Wednesday, November 1st at 11:59pm by e-mail

What to submit

The lab is done in groups of 2 or 3. In the beginning of each file please write (in comments) the names fo all group members.

At the end of the lab please send me and your group partner(s) all your Racket files as e-mail attachments. My e-mail is elenam at morris.umn.edu. The subject of your e-mail must be "1301 Lab 6" followed by "Final" or "Not final", depending on whether this is a final submission or you are still working on it. If you need to finish it, make sure to set up a time with your group partner(s) to finish the lab.

Lab Tasks (20 points)

These problems use recursion. Use the examples we did in class as sample code. All functions must have signatures, descriptions, and check-expects (add check-expects as needed).
Note that list function (used in check-expects below) is an easy way to create a list of several elements without having to repeat cons many times. As an example of how it works, (list 2 3) creates a list (cons 2 (cons 3 empty)).

Question 1 (4 points) Write a function product that consumes a list of numbers and returns their product. It returns 1 for an empty list.

    (check-expect (product (list 2 3 -1)) -6)
    (check-expect (product (list 4 6 0 8)) 0)
    (check-expect (product empty) 1)

Question 2 (4 points) Write a function sum-non-negative that adds up all non-negative numbers in a list.


    (check-expect (sum-non-negative (list -2 3 0 -1 5)) 8)
    (check-expect (sum-non-negative (list 4 8)) 12)
    (check-expect (sum-non-negative (list -1 -2)) 0)

Question 3 (4 points) Write a function count-odd that consumes a list of numbers and returns the number of odd numbers in the list.


    (check-expect (count-odds (list 2 3 1 5 0)) 3)
    (check-expect (count-odds (list 4 6 8)) 0)
    (check-expect (count-odds empty) 0)

Question 4 (4 points) Write a function sum-x-coords that consumes a list of positions and returns the sum of their x coordinates.


    (check-expect (sum-x-coords (list (make-posn 10 20) (make-posn 20 50))) 30)
    (check-expect (sum-x-coords (list (make-posn 10 -20) (make-posn 10 0))) 20)
    (check-expect (sum-x-coords empty) 0)

Question 5 (4 points) Write a function first-odd that consumes a list of numbers and returns the first odd number. Use the predefined function odd? to determine if a number is odd. If no number in the list is odd, return #false.


    (check-expect (first-odd (list 2 3 1 5 0)) 3)
    (check-expect (first-odd (list 4 6 8)) #false)
    (check-expect (first-odd empty) #false)


CSci 1301 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.