;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname lab8-starter-file-1) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) (define-struct user [name friends enemies]) ; A SocialNetwork (SN) is a [Listof User]. ; A User is a (make-user String [Listof String] [Listof String]) ; Interpretation: name is the user's nick name, ; friends is a list of (the names of) their friends, and ; enemies is a list of (the names of) their enemies. (define amanda (make-user "Amanda" '("Carlo" "Diya" "Fran") '())) (define bruce (make-user "Bruce" '("Carlo" "Diya" "Emmi" "Fran" "Han") '("Fran" "Diya" "Carlo"))) (define carlo (make-user "Carlo" '("Diya" "Han") '("Amanda"))) (define diya (make-user "Diya" '("Emmi" "Fran" "Han") '("Fran" "Carlo"))) (define emmi (make-user "Emmi" '("Fran" "Han") '("Carlo" "Diya" "Bruce" "Fran" "Han"))) (define fran (make-user "Fran" '("Gregg" "Han") '("Amanda" "Carlo"))) (define gregg (make-user "Gregg" '() '("Han" "Bruce" "Fran"))) (define han (make-user "Han" '("Gregg") '("Bruce" "Carlo"))) (define SN1 (list amanda bruce carlo diya emmi fran gregg han)) ; LIBRARY FUNCTIONS ----------------------------------------------------------- ; drop : String [Listof String] -> [Listof String] ; Remove all occurrences of the given element from the list, if it is present. (define (drop str los) (filter (λ(x) (not (string=? str x))) los)) (check-expect (drop "hi" '()) '()) (check-expect (drop "hello" '("cats" "are not that affectionate")) '("cats" "are not that affectionate")) (check-expect (drop "cats" '("cats" "hi" "i" "still" "love" "cats" "lots")) '("hi" "i" "still" "love" "lots")) ; count : String [Listof String] -> Number ; Count how many times the given string appears in the list. (define (count str los) (foldr (λ(x y) (if (string=? str x) (+ y 1) y)) 0 los)) (check-expect (count "hi" '()) 0) (check-expect (count "hello" '("cats" "are great")) 0) (check-expect (count "cats" '("cats" "hi" "i" "love" "cats" "lots")) 2)