7.4

Week 6 Set a

home work!

Programming Language ISL

Due Date Monday 10/7 at 9:00pm (Week 6)

Purpose To practice abstracting different versions of functions and using abstractions.

Language change!

As noted above, please submit this assignment in Intermediate Student Language as opposed to BSL+.

Finger Exercises

Exercise 1 From HTDP, 250 252

Graded Exercises

Exercise 2 For this exercise, you may assume the standard definition for Posn and [List-of Posn], and do not need to define them or their templates. You can if you want to, though.

Consider the following functions:
; sum-x-coords : [List-of Posn] -> Number
; Sum all the x-coordinates in the list of positions
(define (sum-x-coords lop)
  (cond
    [(empty? lop) 0]
    [(cons? lop)
     (+ (posn-x (first lop))
        (sum-x-coords (rest lop)))]))
 
(check-expect (sum-x-coords empty) 0)
(check-expect (sum-x-coords
               (cons (make-posn 3 4)
                     (cons (make-posn 5 12)
                           empty))) 8)
 
; mult-distances : [List-of Posn] -> Number
; Multiply all the distances from each position to the origin
(define (mult-distances lop)
  (cond
    [(empty? lop) 1]
    [(cons? lop)
     (* (distance-to-origin (first lop))
        (mult-distances (rest lop)))]))
 
(check-expect (mult-distances empty) 1)
(check-expect (mult-distances
               (cons (make-posn 3 4)
                     (cons (make-posn 5 12)
                           empty))) 65)
 
; distance-to-origin : Posn -> Number
; Produces the distance from this position to the origin
(define (distance-to-origin p)
  (sqrt (+ (sqr (posn-x p)) (sqr (posn-y p)))))
 
(check-within (distance-to-origin (make-posn 2 2)) (sqrt 8) 1e-06)
(check-expect (distance-to-origin (make-posn 3 4)) 5)

  1. Abstract sum-x-coords and mult-distances. Be sure to re-define the functions using your new abstraction.

  2. Use your new function to design the function strings-length, which sums the length of all of the strings in a list of strings.

  3. Next, use the same new function to design the function biggest-difference, which produces the largest difference between a posns’s x and y values in a list of posns. Note that the difference should always be taken as an absolute value, whether the x or y value is bigger.

Exercise 3 Design the function earliest, which takes a non-empty list of strings and a function (that takes two strings and outputs a Boolean), which indicates whether or not the first string comes "before" the second one. The earliest function should output the string that comes earliest in the non-empty list according to the given function. You must define and use a parameterized data definition [NEL X] (non-empty list of X).

Then, use earliest to define three other functions:
  1. One that produces the string that comes earliest lexographically (i.e., which would come first in a dictionary). Hint: string<? is quite useful.

  2. One that produces the string that comes last lexographically.

  3. One that produces the last string in the non empty list.

Exercise 4 Using foldr, design a function that, given a list of strings, returns the same list but with two copies (next to each other in the list, as separate strings) of all strings with an even string-length.