1. Higher-order functions, again!

Here are the signatures of some of the higher-order functions provided by DrRacket.

;; map : [X -> Y] List<X> -> List<Y>
;; andmap: [X -> Boolean] List<X> -> Boolean
;; ormap: [X -> Boolean] List<X> -> Boolean
;; foldr: [X Y -> Y] Y List<X> -> Y
;; foldl: [X Y -> Y] Y List<X> -> Y

You are provided with the following Data Definition

;; An Atom is one of:
;; - Number
;; - Boolean
;; - String
;; - Symbol

Solve the following questions using higher order functions, local and/or lambda

Practice Exercises
  • 1) Given a LoF<Atom> return a list of all the symbols in the input.

  • 2) Given a LoF<Atom> count the number of symbols in the list.

  • 3) Given a LoF<Atom> add on the numerical values in the list and return the total.

  • 4) Given a LoF<Atom> logically or all the boolean values in the input.

  • 5) Given a LoF<LoF<Atom>> filter any empty lists that are elements of the input.

  • 6) Given a LoF<LoF<Atom>> return a LoF<LoF<Symbol>> of all the symbols in the input.

  • 7) Given a LoF<LoF<Atom>> sum all the numerical values in the input.

2. Generic Trees

A friend is working on an assignment that asks them to generalize over trees. They already have examples of tree data definitions

(define-struct node (val kids))
;; A Node<X> is a (make-node X LoF<X>)
;; INTERP: represents a node in a tree

;; An ST is one of
;; - empty
;; - Node<String>


;; An IT is one of
;; - empty
;; - Node<Integer>

Your friend is having problems generalizing over these data definitions.

Practice Exercises
  • 8) Create a generalized data definition for Trees.

Now your friend is asked to generalize over functions of ST and IT.

Practice Exercises
  • 9) Design the function it-sum that takes an IT and adds all the integer values in the IT

  • 10) Design the function st-append that takes an ST and appends all the strings in the ST into one big string.

  • 11) Design a generic function t-op that generalizes over operations on binary trees.

  • 12) Use your t-op to create new implementations, e.g., it-sum2 and st-append2 that use bt-op.

3. Dealing with mouse input in an animation

DrRacket’s big-bang allows programmers to detect mouse operations. The way that we handle mouse events is similar to the way that we handle key events. big-bang has a clause named on-mouse.

The on-mouse clause must be given a function with the signature

;; World Integer Integer MouseEvent -> World

;; A MouseEvent is one of
;; - "button-down"
;; - "button-up"
;; - "drag"
;; - "move"
;; - "enter"
;; - "leave"
The documentation on MouseEvent has more information for each event.

The function given to on-mouse needs to accept a the current World the x and y-coordinate on the canvas where the mouse might have been clicked and a MouseEvent. The function needs to then return the new World after taking into account the mouse event that just occurred.

Here is a small program that you can run and play around with. Clicking inside the canvas should move the red dot to where you click and display the text pressed button. If you click the mouse button and keep it pressed and then move the mouse the red dot should follow your mouse and display the text press and drag.

(require 2htdp/image)
(require 2htdp/universe)


(define BG (empty-scene 500 500))
(define MARK (circle 15 'solid 'red))

(define-struct world (mouse-location message))



(define (draw w)
  (place-image
   (text (world-message w) 20 "blue")
   250
   20
   (place-image MARK
                (posn-x (world-mouse-location w))
                (posn-y (world-mouse-location w))
                BG)))



(define (tock w)
  w)

(define (handle-key w key-event)
 w)

(define (handle-mouse w x y mouse-event)
  (cond
    [(string=? "button-down" mouse-event)
     (make-world (make-posn x y) "pressed button")]
    [(string=? "drag" mouse-event)
     (make-world (make-posn x y) "press and drag")]
    [else (make-world (world-mouse-location w) "")]))

(define (start world)
  (big-bang world
            (on-tick tock)
            (on-key handle-key)
            (on-mouse handle-mouse)
            (to-draw draw)))
Practise Exercise

Design a simplified defender game.

The game starts and a random number of bombs appear on the screen at random locations. The bombs are round and are moving at a steady speed from top to bottom. On the canvas at the location of the mouse there is a target. The target is a black outlined circle. Moving the mouse moves the target on the canvas following the location of the mouse. When the user clicks the mouse any bombs that overlap with the target are destroyed (no longer visible).

If during the game 5 or more bombs reach the bottom of the screen then the game is over.