CSci 1301: Lab 4

You may work in pairs (preferred) or individually.

What to submit

At the end of the lab please send me and your group partner(s) all your Scheme 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.

When to submit

Due Wednesday, October 13th, at 11:59pm. If you submit the final version during the lab, you are done.

Helpful tip

When writing tests for functions, use a predefined function list that takes any number of elements and makes a list out of these elements. For instance


(list 1 2 3)

Results in the list

(cons 1 (cons 2 (cons 3 empty)))

(cons 1 (cons 2 (cons 3 empty)))

Problem 1: Fortune Teller (12 points)

Write a recursive function get-nth that takes a list and a number n > 0 and returns the element at n-th position from the start of the list. Note that the function does not use list elements and therefore should work for lists of any type (or a mix of types).
If there is no element at that position, your function must signal an error with a symbol 'NoSuchElement
Note that your function will have to change both n and the list for the recursive call. It also needs two base cases.
Examples:


(check-expect (get-nth 2 (list 2 3 5 7)) 3)
(check-expect (get-nth 2 (list 4 5)) 5)
(check-expect (get-nth 3 (list 4 5)) 'NoSuchElement)

When the function is done, write a function fortune-teller: it takes a list of "fortunes" (symbols or strings), uses the function random (see in-class examples) to randomly choose a position in the list, and calls get-nth to choose the fortune at that position. (random n) gives numbers from 0 to n-1 so you need to add 1 to the result before a call to get-nth.
For instance, if the fortunes are


(list "Today you will find something precious" 
      "Today is not your day")
and (random 2) gives 0, you select the first element on the list, i.e. "Today you will find something precious".

Problem 2 (6 points)

Exercise 9.5.3 in Section 9.
Note the last line that asks you to generalize the function. The generalized function (price-below-threshold? should work as follows:


(check-expect (price-below-threshold? (list .75 1.95 .25) 1.5) false)
(check-expect (price-below-threshold? (list .75 1.95 .25) 2.0) true)

Problem 3 (8 points)

Write a function remove-element that takes a list of symbols and a symbol and returns a new list that has all occurrences of the given symbol removed.
Examples:


(check-expect (remove-element (list 'A 'B 'A 'C) 'A) (list 'B 'C))
(check-expect (remove-element (list 'A 'B 'A 'C) 'D) (list 'A 'B 'A 'C))
(check-expect (remove-element empty 'A) empty)

CSci 1301 course web site.