Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (require "./remove_duplicates.rkt")
- ; This defines the basic album datatype.
- (define-struct album (title artist genre))
- ; define-struct automatically creates the following functions for you:
- ;
- ; `make-<struct-name>` (in this case `make-album`)
- ; a function to create an instance of the struct
- ; this function takes arguments for each of the fields listed, so for example
- ; (make-struct 'Sway' 'Tove Styrke' 'Pop') will create an album struct
- ; with title 'Sway', artist 'Tove Styrke' & genre 'Pop
- ;
- ; `<struct-name>-<field-name>` (for each field)
- ; functions for accessing values of each field in the struct
- ; for album this would mean we'd have the following functions:
- ; `album-title`, `album-artist`, `album-genre`
- ; the following examples creates an album and then accesses its fields
- ; ```
- ; (define sway (make-album 'Sway' 'Tove Styrke' 'Pop')
- ; (album-title sway) ; returns 'Sway'
- ; (album-artist sway) ; returns 'Tove Styrke'
- ; (album-genre sway) ; returns 'Pop'
- ; ```
- ;
- ; `<struct-name>?` (in this case `album?`)
- ; a predicate (function which returns a boolean) that checks a value and
- ; returns true if it's an instance of the struct, false otherwise
- ; using the `sway` album defined in the previous example
- ; ```
- ; (album? sway) ; returns true
- ; (album? 1) ; returns false
- ; (album? 'hi') ; returns false
- ; ```
- ;;; Enter a list of albums below
- ;;; They need not be the actual albums you own.
- ;;; But you should include enough variety to adequately
- ;;; test your code.
- ;;;
- ;;; Here's what we mean. One of the questions involves
- ;;; writing a procedure that finds all the albums of a
- ;;; given genre. If all the albums in the library are
- ;;; in the rock genre, then there's only one genre and
- ;;; when you ask for all the rock albums and it gives
- ;;; back all the albums, you don't know whether that's
- ;;; because the code really works, or because it's
- ;;; not even paying attention to the genre. So you want
- ;;; to make sure there are multiple artists and genres,
- ;;; some artists with only one album or genre, others
- ;;; with multiple artists or genres, etc.
- (define testing-library-1
- ;; Fill in the info below
- (list (make-album "In My Head" "Jason Derulo" "Pop")
- (make-album "Baby" "Justin Bieber" "Pop")
- (make-album "Roses" "Chainsmokers" "EDM")
- (make-album "Broken Arrows" "Avicii" "EDM")
- (make-album "Pursuit of Hapiness" "Kid Cudi" "Banger")
- (make-album "Wake Me Up" "Avicii" "Banger"))
- )
- ;;; Add the procedures you write (e.g. all-genres, versatile-artists)
- ;;; below. Be sure to test your procedures to make sure they work.
- ;;; We are not providing test cases this time, so it's up to you
- ;;; to make sure your code works. We will use our own test cases
- ;;; when grading and assign you a grade based on the number of
- ;;; test cases that passed.
- ; all-titles : (listof album) -> (listof string)
- ; Gets all titles of all albums in the library
- (define all-titles
- (lambda (lib) (map (λ (album) (album-title album)) lib)))
- (check-expect
- (all-titles
- (list (make-album "a" "somebody" "rock") (make-album "b" "somebody else" "country") (make-album "c" "asd" "asd")))
- (list "a" "b" "c"))
- ;; all-artists: (listof album) -> (listof string)
- ; Gets all the artists from the albums in the library
- (define all-artists
- (lambda (lib) (remove-duplicates (map (λ (album) (album-artist album)) lib))))
- (check-expect
- (all-artists
- (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "country") (make-album "c" "c" "country")))
- (list "a" "b" "c"))
- ;; all-genres: (listof album) -> (listof string)
- ; Gets all the genres from the albums in the library
- (define all-genres
- (lambda (lib) (remove-duplicates (map (λ (album) (album-genre album)) lib))))
- (check-expect
- (all-genres
- (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "c" "country")))
- (list "rock" "country" "pop"))
- ;; artist-albums : string, (listof album) -> (listof album)
- ; Gets all the albums an artist made from the library
- (define artist-albums
- (lambda (desired-artist lib) (map (λ (n) (album-title n)) (filter (lambda (album)
- (string=? (album-artist album) desired-artist)) lib))
- ))
- (check-expect
- (artist-albums "a"
- (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "c" "country")))
- (list "a" "b"))
- ;; artist-genres: string, (listof album) -> (listof string)
- ; Gives all the genres of a given artist from the library
- (define artist-genres
- (lambda (desired-artist lib) (map (λ (n) (album-genre n)) (filter (lambda (album)
- (string=? (album-artist album) desired-artist)) lib))
- ))
- (check-expect
- (artist-genres "a"
- (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "c" "country")))
- (list "rock" "country"))
- ;; artist-is-versatile?: string, (listof album) -> boolean
- ; Determines whether an artist has albums in more than 1 genre in the library
- (define artist-is-versatile?
- (lambda (desired-artist lib) (> (length (artist-genres desired-artist lib)) 1)))
- (check-expect
- (artist-is-versatile? "a"
- (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "c" "country")))
- #true)
- (check-expect
- (artist-is-versatile? "b"
- (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "c" "country")))
- #false)
- ;; versatile-artists: (listof album) -> (listof string)
- (define versatile-artists
- (λ (lib) (remove-duplicates (map (λ (albums) (album-artist albums)) (filter (λ (album) (artist-is-versatile? (album-artist album) lib)) lib)))))
- (check-expect
- (versatile-artists
- (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "b" "country")))
- '("a" "b"))
- (check-expect
- (versatile-artists
- (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "country") (make-album "c" "b" "country")))
- '("a"))
- ; artist-album-count: string, (listof album) -> number
- (define artist-album-count
- (λ (artist lib) (length (filter (λ (album) (string=? (album-artist album) artist)) lib))))
- (check-expect
- (artist-album-count
- "a"
- (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "b" "country")))
- 2)
- ; artist-album-count-list: string, (listof album) -> (listof string number)
- (define artist-album-count-list
- (λ (artist lib) (list artist (artist-album-count artist lib))))
- (check-expect
- (artist-album-count-list
- "a"
- (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "b" "country")))
- (list "a" 2))
- ;; artist-album-counts: (listof album) -> (listof (list string number))
- (define artist-album-counts
- (λ (lib)
- (remove-duplicates (map (λ (album) (artist-album-count-list (album-artist album) lib)) lib))))
- (check-expect
- (artist-album-counts
- (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "b" "country")))
- (list (list "a" 2) (list "b" 2)))
- ; genre-album-count: string, (listof album) -> number
- (define genre-album-count
- (λ (genre lib) (length (filter (λ (album) (string=? (album-genre album) genre)) lib))))
- (check-expect
- (genre-album-count
- "country"
- (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "b" "country")))
- 2)
- ; genre-album-count-list: string, (listof album) -> (listof string number)
- (define genre-album-count-list
- (λ (genre lib) (list genre (genre-album-count genre lib))))
- (check-expect
- (genre-album-count-list
- "country"
- (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "b" "country")))
- (list "country" 2))
- ;; genre-album-counts: (listof album) -> (listof (list string number))
- (define genre-album-counts
- (λ (lib)
- (remove-duplicates (map (λ (album) (genre-album-count-list (album-genre album) lib)) lib))))
- (check-expect
- (genre-album-counts
- (list (make-album "a" "a" "rock") (make-album "b" "a" "country") (make-album "b" "b" "pop") (make-album "c" "b" "country")))
- (list (list "rock" 1) (list "country" 2) (list "pop" 1)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement