8.15

Homework 7🔗

home work!

Programming Language ISL!!

Due Date: Friday February 28, 9pm

Purpose To practice using list abstractions

Expectations

-----------------------------------------------------------------------------

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.