;; 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 week7_tu_full) (read-case-sensitive #t) (teachpacks ((lib "image.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.ss" "teachpack" "htdp"))))) ; an invention-event is a: ; - symbol inventor ; - symbol item ; - number year (define-struct invention-event (inventor item year)) ; examples: (make-invention-event 'edison 'lightbulb 1879) (make-invention-event 'watt 'steam-engine 1765) ; a list-of-invention-events is either: ; - empty ; - (cons invention-event list-of-invention-events) (define myinventions (list (make-invention-event 'edison 'lightbulb 1879) (make-invention-event 'edison 'phonograph 1877) (make-invention-event 'watt 'steam-engine 1765) (make-invention-event 'dr-evils-dad 'question-mark 1560))) ; Q1: It's time to get the credit you so richly deserve -- use lambda and map to write a (single) statement that outputs myinventions with all the inventors switched to be you. (map (lambda (a) (make-invention-event 'doug (invention-event-item a) (invention-event-year a))) myinventions) ; Q2: Write a function select-in-years using filter that returns all inventions within a specified year range. ; select-in-years: list-of-invention-events number number -> list-of-invention-events ; returns all invention events that happened between specified years (inclusive) (define (select-in-years loi ystart yend) (filter (lambda (x) (and (>= (invention-event-year x) ystart) (<= (invention-event-year x) yend))) loi)) (check-expect (select-in-years myinventions 1870 1890) (list (make-invention-event 'edison 'lightbulb 1879) (make-invention-event 'edison 'phonograph 1877))) (check-expect (select-in-years myinventions 1970 1990) empty) ; a hq is a: ; - symbol company ; - symbol city (define-struct hq (company city)) ; examples: (make-hq 'intel 'santa-clara) (make-hq 'nvidia 'santa-clara) (make-hq 'google 'mountain-view) (define myhqs (list (make-hq 'intel 'santa-clara) (make-hq 'nvidia 'santa-clara) (make-hq 'google 'mountain-view))) ; Q3: Consider the following function, select-unique-city, which selects cities from a list-of-hq, eliminating duplicates: ; select-unique-city : list-of-hq -> list-of-symbol ; returns all cities in the input list, eliminating duplicates (define (select-unique-city lohq) (remove-duplicates (map hq-city lohq))) ; remove-duplicates : list-of-X -> list-of-X ; Removes the symbols that appear more than once, leaving ; the relative order of the others unchanged (define (remove-duplicates lox) (cond [(empty? lox) empty] [(cons? lox) (cons (first lox) (remove (first lox) (remove-duplicates (rest lox))))])) (check-expect (select-unique-city myhqs) (list 'santa-clara 'mountain-view)) (check-expect (select-unique-city empty) empty) ; Design a similar function select-unique-inventor, which selects unique inventors from a list-of-invention-events. ; select-unique-inventor : list-of-invention-event -> list-of-symbol ; returns all inventors in the input list, removing duplicates (define (select-unique-inventor loie) (remove-duplicates (map invention-event-inventor loie))) (check-expect (select-unique-inventor myinventions) (list 'edison 'watt 'dr-evils-dad)) (check-expect (select-unique-inventor empty) empty) ; Q4: Using select-unique-city and select-unique-inventor above, write an abstract function select-unique-values that can be used to return all ; unique values of some field (e.g. inventor, company) from a list of data of some type (e.g. invention-events, hqs). ; select-value : (X -> Y) list-of-X -> list-of-Y ; returns unique values produced by given selector to given list (define (select-unique-values selector lox) (remove-duplicates (map selector lox))) (check-expect (select-unique-values hq-city myhqs) (list 'santa-clara 'mountain-view)) (check-expect (select-unique-values hq-city empty) empty) (check-expect (select-unique-values invention-event-inventor myinventions) (list 'edison 'watt 'dr-evils-dad)) (check-expect (select-unique-values invention-event-inventor empty) empty)