6.10

Assignment 4

home work!

Programming Language BSL

Due Dates:
  • Tests for exercises 8, 9 and 10: Friday 2/2 at 9:00pm

  • Full solutions: Tuesday 2/6 at 9:00pm

Purpose To practice designing functions on lists.

Finger Exercises

Exercise 4 Design the function du, which consumes a list of strings and computes their total length.

Hint: Remember string-length.

Graded Exercises

Exercise 5 Picture frames

Design a function that takes a list of images and frames them all. That is, it turns this:

(cons image (cons image (cons image (cons image '()))))

into this:

(cons image (cons image (cons image (cons image '()))))

Exercise 6 Image areas

Design a function that can take a list of images and produce a number representing their total area. (Remember: a picture’s area depends only on its size, not on its contents. The circle and square in the images above have the same width and height, and so have the same area, despite appearing to be different shapes. The triangle and star have the same height as the other shapes, but different widths; they definitely have different areas.)

Exercise 7 Trailing off...

Design a function that, for example, can turn this input:

(cons image (cons image (cons image (cons image '()))))

into this example output:

image

A longer list would produce something like

image

Specifically, the function should take a list of images and place them beside each other, aligned at the bottoms, and such that the composite images are shrunk by some scale factor (here I used 0.9). There’s nothing special about the images themselves; the example images above used the answer to the Picture Frames exercise above, so you can easily see the sizes of the component images.

Exercise 8 Design a function eliminate, that takes a List of Numbers lon and a Number n, and constructs a new List of Numbers with all the same values as lon except for any numbers equal to n. For instance,

(check-expect (eliminate (cons 5 (cons 4 (cons 5 (cons 2 '())))) 5)
              (cons 4 (cons 2 '())))

Note: this is essentially remove-all, simplified to lists of only numbers. We have not addressed how equal? works yet, so the full flexibility of remove-all doesn’t yet make sense. Also – do not use remove-all to implement this function!

Challenge (optional): Design a function no-dups, that takes a List of Numbers, and constructs a new list with any duplicate values removed (and keeping just a single one of the copies). For example,
(check-expect (no-dups (cons 5 (cons 4 (cons 5 (cons 2 '())))))
              (cons 5 (cons 4 (cons 2 '()))))

Exercise 9 We know that cons allows us to add a number to the front of a list of numbers. Design its opposite, snoc, that allows us to add a number onto the end of a list of numbers.

Exercise 10 Design a function search-for-string that takes a string and a list of strings, and returns a list containing all the strings in the given list that contain the given string as a substring.

Hint: There are other string-processing functions besides substring; one of them is very useful for this problem. Look up the documentation for substring, and scroll up through the documentation to find a function that might help with checking string containment...