Assignment 8
Due Date : 12/06 @ 11:59pm
Note
For each function that you design you are expected to follow the Design Recipe in the same way as we did in class. Failure to show all your work for each step will cost you points.
Problem 1
Write a function find-string
that takes
in a Listof[String]
and a String
and that returns a Boolean
, true
if and only if the given string was in the list.
Abstract find-string
to generic-find-string
so that
the string comparison operation it uses is a
parameter. Then use this abstraction to define
find-string-case-sensitive
, which should operate
the same way as the original find-string
, and
find-string-case-insensitive
, which has the same
contract as find-string
but which ignores the case
of alphabetic characters when comparing strings
(i.e. the character a is considered the same as A
and so on; non-alphabetic characters must still
match exactly).
Problem 2
DrRacket has lots of abstract functions for processing lists (pg. 313, Sect. 21.2, or the online version).
Given the following data definitions:
;; A Grade is: (make-grade Symbol Number) (define-struct grade (letter num)) ;; The Symbol in a Grade represents ;; 'A >= 90 ;; 'B >= 80 ;; 'C >= 70 ;; 'D >= 60 ;; 'F < 60 ;; A Listof[Grades] ... (define grades (list (make-grade 'D 62) (make-grade 'C 79) (make-grade 'A 93) (make-grade 'B 84) (make-grade 'F 57) (make-grade 'F 38) (make-grade 'A 90) (make-grade 'A 95) (make-grade 'C 76) (make-grade 'A 90) (make-grade 'F 55) (make-grade 'C 74) (make-grade 'A 92) (make-grade 'B 86) (make-grade 'F 43) (make-grade 'C 73)))
Design the requested functions to manipulate Grades. You must use the given list as one of your tests. For each you may use a local function.
Note: if you do not use the DrRacket loop function mentioned, you will not recieve credit for the sub-problem!
-
Design the function
log->lon
that converts aListof[Grade]
into aListof[Number]
that contains just the numerical grade, using the Scheme functionmap
. -
Using
foldr
, design the functionbest-grade
that finds the highest Grade in aListof[Grade]
. -
Design a function
just-As
that returns a list of only the 'A grades, usingfilter
. -
Use
andmap
to design the functionall-pass?
that checks to see if all the Grades in a given list are not'F
. -
Finally design the function
bonus
that adds 5 to all of the Grades in a given list, and updates the letter portion of the Grade if it changes. Usemap
to design your function... it must return aListof[Grade]
!
Problem 3
Use local and "loops" (abstractions such as map
,
filter
, foldr
, etc) wherever
your functions may benefit from them, especially for
the lists of objects in your Space Invaders game.