CSci 1301: Lab 4

Due: Friday, October 23rd at 11:59pm by e-mail

What to submit

The lab is done in groups of 2. 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 4" 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.

Task 1 (16 points)

Problems in this task do not require recursion. You may not use any list functions other than first, rest, cons, empty?, list. All functions must have contracts and check-expects.

Question 1 Write a function sum-of-first-two that, given a list of numbers of length at least two, returns the sum of the first and the second elements of the list:


(check-expect (sum-of-first-two (list 2 3 5 6)) 5)
  
Do not worry about a list that has fewer than two elements.

Question 2 Write a function has-one-element? that, given a list, returns #true if the list has exactly one element and #false otherwise:


	  (check-expect (has-one-element? (list 2 3 5 6)) #false)
	  (check-expect (has-one-element? empty) #false)
	  (check-expect (has-one-element? (list 2)) #true)
  

Question 3 Write a function has-more-than-one-element? that, given a list, returns #true if the list has more than one element, and #false otherwise:


	  (check-expect (has-more-than-one-element? (list 2 3 5 6)) #true)
	  (check-expect (has-more-than-one-element? empty) #false)
	  (check-expect (has-more-than-one-element? (list 2)) #false)
  

Question 4 Write a function order that, given two numbers,, returns a list of them in increasing order. If they are equal, the list just has these two numbers:


      (check-expect (order 2 5) (cons 2 (cons 5 empty)))
      (check-expect (order 4 1) (cons 1 (cons 4 empty)))
      (check-expect (order 2 2) (cons 2 (cons 2 empty)))
  

Task 2 (8 points)

These problems use recursion. Use the examples we did in class as sample code.

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)

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)


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.