CSci 1301: Lab 8

You may work in groups of 2 (preferred) or individually.

What to submit

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 8" 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 Monday, November 29th, at 11:59pm. If you submit the final version during the lab, you are done.

Problem 1 (10 points)

Question 1 Write a function filter-function-builder that builds filter functions, similarly to a building function for map or foldr here. Your function must take one parameter (a filtering criterion which is a function X -> boolean) and returns a function that filters a list of type X according to that criterion. For instance, using the filtering function builder allows us to create a function to filter out all odd numbers by passing even? to a filter-builder.

You may use local or lambda to define the function.

Question 2

Use your function to define the following functions. Use lambda to define the criterion function that is passed to filter-builder.

Problem 2 (8 points)

The following starting code includes a circle structure and its draw and clear functions and some helpful functions:


;; The structure represents a circle 
;; center is the position (posn structure) of the center
;; radius is its radius (a non-negative number), and
;; color is a symbol representing a color
(define-struct circle (center radius color))

;; draw-a-circle: circle -> true
;; the function draws a circle structure as a disk
;; and returns true
(define (draw-a-circle a-circle)
  (draw-solid-disk (circle-center a-circle)
                   (circle-radius a-circle)
                   (circle-color a-circle)) 
)

;; clear-a-circle: circle -> true
;; the function clears a disk corresponding to a circle
;; and returns true
(define (clear-a-circle a-circle)
  (clear-solid-disk (circle-center a-circle)
                   (circle-radius a-circle))
)


(define colors (list 'red 'green 'blue 'magenta 'cyan 'yellow 'orange 'purple))

(define (nth a-list n)
  (cond
   [(= n 0) (first a-list)]
   [else (nth (rest a-list) (- n 1))]
  )
)

Write a function random-circle: integer -> circle that takes an integer number (which is not going to be used!!!) and creates a circle with a random position on the canvas, random radius, and a random color (use nth with a random parameter to select a color, see above). Test it by calling it with any integer parmeter and draw the resulting circle: (draw-a-circle (random-circle 5)).

Use build-list to build a list of 50 circles, store the list in a variable (you will need to clear the circles later). Then use map to map the draw-a-circle. This will draw all the circles.

Use sleep-for-while, then clear the circles list by mapping the clear function over the list. Then create another list and draw and clear it using map.

Problem 3 (10 points)

A prime number is defined as a positive integer greater than 1 divisible only by 1 and itself. The first five prime numbers are: 2, 3, 5, 7, 11. A common approach for generating prime numbers is known as Sieve of Eratosthenes. The algorithm starts off with a list of all integers up to some given N and then proceeds to "cross out" numbers that are divisible by 2, 3, 4, etc. The largest number that it needs to cross out is the sqaure root of N rounded up.

Use build-list to generate a list of all numbers from 2 to 1000. Use lambda to define a function passed to build-list.

Write a recursive function that takes a list, integer n, and a maximum integer. If n exceeds the maximum, the list is returned. Otherwise the function filters out numbers that are divisible by n from the result of the recursive call with n+1.

The criteria to filter must be writtten using lambda. Be careful when writing the criteria: make sure that you are not filtering out numbers that are divisible by themselves, or you will get an empty list.

Call the function with the list of numbers from 2 to 1000, 2 as the starting value of n, and square root of 1000 as the maximum. The resulting list will have all prime numbers between 2 and 1000.

Use Intermediate Student with lambda language level for this lab.


CSci 1301 course web site.