CS U211 - Lab 9



Scheme Built-in Abstract Functions

Exercise 1:

Review the functions here. Read and know the Contracts!!

Here's some definitions...

;; A Pt is (make-pt Number Number)
(define-struct pt (x y))

;; Screen Width and Height
(define scr-size 400)

;; MAX is the maximum point to be generated/drawn
(define MAX 50)

;; pt-size is the size (radius) of each point
(define pt-size (/ scr-size MAX))


;; rand: Number -> Number
;; Ignore the argument, return a random number [0..99]
(define (rand dummy)
  (random MAX))
    
Using those definitions, develop the following functions using the requested Scheme abstract function.

** Remember to use local to define helper functions within your main definitions.

Exercise 2:

Implement the function, make-points, that makes a (listof Pt). Use the Scheme function build-list.

Here's the skeleton:

;; make-points: (Number -> Number) -> (listof Pt)
;; Create a List of Pts that contains points with Xs from 0..(- MAX 1)
;;   and Ys from (f x) for each x scaled by pt-size.  In other worlds:
;;       (list (make-pt 0 (* (f 0) pt-size) ...
;;             (make-pt (* (- MAX 1) pt-size) (* (f (- MAX 1)) pt-size)))
(define (make-points f) ...)
          

Exercise 3:

Implement the function, draw-points, that draws a (listof Pt). Use the Scheme function foldr.

Here's the skeleton:

;; draw-points: (listof Pt) Symbol -> Scene
;; Draw the list of points into an empty scene using
;;   scr-size for the scene dimensions
(define (draw-points lop color) ...)
          

Exercise 4:

Implement the function, center-points, that moves the y of each Pt towards the center. Use the Scheme function foldr.

Here's the skeleton:

;; center-points: (listof Pt) -> (listof Pt)
;; Move all points ys half-way toward the center
(define (center-points lop) ...)
          

Here's some starter code to test your functions...

;; do-char: Char (listof Pt) -> (listof Pt)
;; Make changes/create a new List based on a key press
(define (do-char ch lop)
  (cond [(char=? ch #\r) (make-points rand)]
        [else lop]))

(define (draw lop) (draw-points lop 'red))
(define (key lop s/ch)
  (cond [(char? s/ch) (do-char s/ch lop)][else lop]))
(big-bang scr-size scr-size 1 '())
(on-redraw draw)
(on-key-event key)
      

Exercise 5:

Modify do-char so that pressing the character C on the keyboard centers the points one step (half-way) towards the center of the screen (e.i., call center-points)

A little more interesting

Use your code above to add some features to the graph program.

Exercise 6:

Implement the function, filter-points, that removes any points that have a y coordinate more than (* n 20) away from the center. Use the Scheme function filter.

Here's the skeleton:

;; filter-points: (listof Pt) Number -> (listof Pt)
;; Remove the points that are not within (* n 20) of the center
;;   Y of the screen
(define (filter-points lop n) ...)
          

Exercise 7:

Modify the do-char function so that when you hit the number keys #\1 ... #\9 the list of Pts is filtered by the corresponding numbers: (1 ... 9).

Exercise 8:

Write some Math functions (Number -> Number) and attach them to different keys so that the (listof Pt) is replaced with one generated by your function(s). Be sure to filter the points to only those that will be displayed on the screen.

Challenge

Exercise 9:

Change the representation of the world to include a Number: the index of the currently selected point.

Something like:

;; A World is (make-world Number (listof Pt))
(define-struct world (i lop))

Exercise 10:

Modify the draw function to put a Blue Circle over the selected point, if the index is within range: 0 <= index <= (length lop)). If not, just draw the list of points.

Use the function list-ref to get the correct point, as long as it's in range (see the HelpDesk for info)

Exercise 11:

Modify key to allow the 'left and 'right keys to move the currently selected point.

Test your modifications with a few different functions

Exercise 12:

Once you're able to move the point, add text to the top left corner of the screen that displays the coordinates of the currently selected point (if valid of course).

Remember text?.