;;; Name: David Evans
;;; Date: 3 February 2004
;;; CS 200 PS2
(load "election.ss")
;;; Question 1a
(define vertical-mid-line
(lambda (t) (make-point 0.5 t)))
;;; Question 1b
(define vertical-line
(lambda (x)
(lambda (t)
(make-point x t))))
;;; Question 2a
(define virginia-electorate
(lambda (pos)
(+ 0.3 (/ pos 2))))
;;; Question 2b
(define (square x) (* x x))
(define charlottesville-electorate
(lambda (pos)
(if (< pos 0.25)
(- 0.8 (* 10 (square (abs (- pos 0.1)))))
(if (< pos 0.6)
(- 0.575 (- pos 0.25))
(+ 0.225 (square (- pos 0.6)))))))
;;; Question 3:
(define (electorate-voters-worker electorate start-position end-position step)
(if (> start-position end-position)
0
(+ (electorate start-position)
(electorate-voters-worker electorate (+ start-position step)
end-position step))))
;;; Another way to solve question 3 would be to use for (defined in Lecture 9):
(define (for index stoptest combine next accum)
(if (stoptest index)
accum
(for (next index) stoptest combine next (combine index accum))))
(define (electorate-voters electorate start-position end-position)
(for start-position
(lambda (pos) (> pos end-position)) ;; stop when pos > end-position
(lambda (pos accum) (+ (electorate pos) accum))
(lambda (pos) (+ pos (/ 1 electorate-steps))) ;; increment by electorate-steps
0)) ;; initially, accumulator is 0
;;; Question 4:
(define (twice f) (compose f f))
;;; alternately: (define (twice f) (lambda (x) (f (f x))))
;;; or: (define (twice f) (n-times f 2))
;;; Question 5:
(define (n-times f n)
(if (= n 0)
(lambda (x) x)
(compose f (n-times f (- n 1)))))
;;; Question 6:
; There were lots of easy ways to win the election, which many of you found.
; The intent of this question was to get you thinking creatively and doing
; something more interesting than just changing the constants in the provided
; code, but I guess it wasn't worded clearly enough to encourage you to do
; that.