CSci 1301: Lab 5

You may work in groups of 2 or 3 (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 5" 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 14th, at 11:59pm. If you submit the final version during the lab, you are done.

Problem 1 (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:


(remove-element (cons 'A (cons 'B (cons 'A (cons 'C empty)))) 'A)
;; expected value
(cons 'B (cons 'C empty))

(remove-element (cons 'A (cons 'B (cons 'A (cons 'C empty)))) 'D)
;; expected value
(cons 'A (cons 'B (cons 'A (cons 'C empty))))

(remove-element empty 'A)
;; expected value
empty

Problem 2: 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 "No such element"
Note that your function will have to change both n and the list for the recursive call. It also needs two base cases.
Examples:


(get-nth 2 (cons 2 (cons 3 (cons 5 (cons 7 empty)))))
;; expected value 
3

(get-nth 2 (cons 4 (cons 5 empty)))
;; expected value 
5

(get-nth 3 (cons 4 (cons 5 empty)))
;; expected value 
get-nth: No such element

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


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


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.