Homework 7
Due Date: Friday February 28, 9pm
Purpose To practice using list abstractions
Expectations
You should submit two .rkt files, each containing your response to one of the 2 exercises below, via the Handin Server; the two files should be named "HW7-1.rkt" and "HW7-2.rkt". We accept NO email submissions. Failure to submit a .rkt file will result in a 0 for that part.
You are only allowed to use the language specified at the top of this page: failure to do so will result in a 0. For this assignment, you must use ISL (remember to correctly set the mode in DrRacket). Using the wrong language will invalidate your submission and result in a score of 0, so please be careful.
Your code must conform to the guidelines outlined in the style guide on the course website. Pay particular attention to the list of prohibited functions in the guide. The style guide may be updated as the semester progresses so please remember to read it before submitting each assignment.
Unless otherwise stated, all data and functions you design must be designed according to the data and function design recipes (DR), resp., following all four steps in each case, with the exception of functions that call big-bang, for which you need no check-expects, as justified in class. All functions used inside big-bang must have check-expects, along with the other three steps of the design recipe.
-----------------------------------------------------------------------------
Exercise 1 Abstracting Functions: Consider the two functions below (we have deliberately omitted tests and purpose statements):
; until-zero: [List-of Number] -> [List-of Number] (define (until-zero lon) (cond [(empty? lon) empty] [(cons? lon) (if (= (first lon) 0) empty (cons (first lon) (until-zero (rest lon))))])) ; words-until-period: [List-of String] -> [List-of String] (define (words-until-period los) (cond [(empty? los) empty] [(cons? los) (if (string=? (first los) ".") empty (cons (first los) (words-until-period (rest los))))]))
Design a list abstraction that can be used to simplify the two functions above.
Use the list abstraction you just designed to rewrite the function until-zero from above.
Use the list abstraction you just designed to rewrite the function words-until-period from above.
Exercise 2 Using ISL List Abstractions: This exercise focuses on applications of ISL’s built-in list abstractions, mainly map, filter, and foldr. Each part will involve using one or more list abstractions in some combination. They might require writing additional helper functions: you should apply the usual design recipe for each of those. Points will be deducted if you did not use list abstractions where it would have clearly made the code more concise.
Another important note: according to our style guide, unless explicitly allowed for some particular exercise (none in this homework), you are not allowed to use the functions equal?, member?, or remove. These all use equal? internally, which has some strange properties we need to avoid.
For all the parts of this exercise, refer to the following data definition used to represent a community of users on an online neighborhood community website:
(define-struct user [username friends rating]) (define-struct proctor [username friends community rank]) ; A SiteMember is one of: ; - (make-user String [List-of String] NatNum) ; - (make-proctor String [List-of String] String NatNum) ; and represents either: ; - a regular user's login username, friends' usernames, and community rating ; - a site supervisor's login user name, friends' usernames, community they manage, ; and their supervisory rank (define SM-USER-1 (make-user "fundies1" (list "jo6n") 2)) (define SM-USER-2 (make-user "jo6n" (list "fundies1" "adam12") 4)) (define SM-PROCTOR-1 (make-proctor "agent86" (list "adam12" "agent99") "Control Agents" 99)) (define SM-PROCTOR-2 (make-proctor "adam12" (list "agent86" "fundies1") "CHIPs" 12)) ; sitemember-temp : SiteMember -> ? (define (sitemember-temp sm) (... (cond [(user? sm) (... (user-username sm) (los-temp (user-friends sm)) (user-rating sm) ...)] [(proctor? sm) (... (proctor-username sm) (los-temp (proctor-friends sm)) (proctor-community sm) (proctor-rank sm) ...)])))
Design the function only-proctors that accepts a list of site members, and produces a list of only the proctors.
Design the function all-friends that returns a single list of all of the friends mentioned by anyone in the list of site members. You do not need to remove duplicates, if any exist. Hint: You might want to take a look at the append function.
Design the function num-users that counts the number of regular users (not proctors) in a list of site members.
Design the function popular? that determines if at least one person in a site member list has more than one friend.
Design the function most-popular that computes for a list of site members the number of friends the member with the most friends has.
Design the function highest-rating that computes for a list of site members the rating of the user with the highest rating.