6.6

Assignment 12

home work!

Programming Language ISL

Due Date Thursday 03/14 at 9pm

Purpose To practice local definitions, scope, and abstracting functions.

Expectations
  • You should submit a single .rkt file containing your responses to all exercises via the Handin Server. We accept NO email submissions. Failure to submit a .rkt file will result in a 0.

  • You are only allowed to use the language specified at the top of this page: failure to do so will result in a 0.

  • Your code MUST conform to the guidelines outlined in the style guide on the course website. The style guide will be updated as the semester progresses so please remember to read it before submitting each assignment.

  • You must follow all the steps of the design recipe when completing this assignment.

  • Please be sure to look at the feedback for assignments 10 and 11 before submitting, as we will be grading you more harshly on things we have warned you about before.

  • You must submit this assignment with your partner. Please make sure you can submit with your partner before 5pm on Thursday or we cannot guarantee that you will be able to submit your homework before the deadline. If the course staff are unable to assist you after 5pm and this causes your homework to be late we will not grant an extension.

Understanding local and scope

The following exercises will ask you to complete the steps of the function design recipe. Remember that the design recipe is available here on the course webpage and here on Piazza. The steps of the function design recipe include: signature, purpose statement, tests, and code. Since we have provided code for each exercise you should only be concerned with the other 3 steps.

Exercise 1 Complete the steps of the function design recipe for the following function:
(define (x y z)
  (local [(define (x z)
            (+ z y))]
    (x z)))

Exercise 2 Write a signature and a set of representative check-expects for the following function:
(define (a b c d)
  (local [(define e (+ d c))]
    (cond [(empty? b) (* e d)]
          [(cons? b) (a (rest b) (first b) e)])))
You do NOT have to provide a purpose statement.

Exercise 3 Consider the following code:
(define (q r)
  (local [(define (s t u) (* (sqr r) (sqrt t) u))] s))
(define (x y z) (* 4 (sqrt y) z))
(check-expect (q 2) x)
Why won’t this code run in DrRacket? We are not just looking for the error message here, but a conceptual explanation of why this task is impossible.

Exercise 4 Complete the steps of the function design recipe for the following function:
(define (adder x)
  (local [(define (f z) (+ x z))] f))

Abstracting functions

Exercise 5 Consider the following functions:
; A ListOfPosns is one of:
; - '()
; - (cons Posn ListOfPosns)
; and represents a list of positions on the x-y plane
 
(define LOP-EMPTY '())
(define LOP-NON-EMPTY (cons (make-posn 8 6) (cons (make-posn 1 2) '())))
 
; sum-x-coords : ListOfPosns -> Number
; Sum all the x-coordinates in the list of positions
(check-expect (sum-x-coords LOP-EMPTY) 0)
(check-expect (sum-x-coords LOP-NON-EMPTY) 9)
(define (sum-x-coords lop)
  (cond [(empty? lop) 0]
        [(cons? lop) (+ (posn-x (first lop)) (sum-x-coords (rest lop)))]))
 
; mult-distances : ListOfPosns -> Number
; Multiply all the distances from each position to the origin
(check-expect (mult-distances LOP-EMPTY) 1)
(check-within (mult-distances LOP-NON-EMPTY) (* 10 (sqrt 5)) 1e-06)
(define (mult-distances lop)
  (cond [(empty? lop) 1]
        [(cons? lop) (* (distance-to-origin (first lop))
                        (mult-distances (rest lop)))]))
 
; distance-to-origin : Posn -> Number
; Produces the distance from this position to the origin
(check-expect (distance-to-origin (make-posn 3 4)) 5)
(check-within (distance-to-origin (make-posn 10 5)) (sqrt 125) 1e-06)
(define (distance-to-origin p)
  (sqrt (+ (sqr (posn-x p)) (sqr (posn-y p)))))
Design the function process-lop which abstracts sum-x-coords and mult-distances. Then redefine both sum-x-coords and mult-distances using process-lop. You do not need to copy in the original definition of either function but remember to complete all the steps of the design recipe.

Exercise 6 Use process-lop (which you designed in the previous exercise) to design the function sum-ratios which takes a ListOfPosns and produces a single Number which is the result of adding up the ratio of the x-coordinate to the y-coordinate of all the Posns. Some check-expects have been provided for you below:
(check-expect (sum-ratios '()) 0)
(check-expect (sum-ratios (cons (make-posn 10 5) '())) (/ 10 5))
(check-expect (sum-ratios (cons (make-posn 10 5) (cons (make-posn 2 3) '())))
              (+ (/ 10 5) (/ 2 3)))
REMINDER: You MUST use process-lop to define your function.