-1

enter image description hereWrite the function build-author-index that consumes a(listof Book) and a list of unique authors (Strings). The function produces an AuthorIndex where the keys are the authors consumed (in the same order) and the values are the titles of all books by that author in the list of Book (also in the same order).

The following is my code. I have to use Beginning Student with List Abbreviations. I am only allowed to use cons, first, second, third, rest, empty?, eq? cons?, list, member?, and length.

posting an image of my code since stack overflow does not allow me to

Coder
  • 151
  • 2
  • 12
  • Stack Overflow has the `{}` that will format text. What do you think `(first a b c d)` is supposed to do when it originally takes only one argument? (else in `author-index-list`) Is it ok that `(author-index-list '() my-bookshelf) ; ==> my-bookshelf`? – Sylwester Oct 15 '19 at 18:05

1 Answers1

0
#lang racket

(define my-bookshelf
  '((book-name-1 author-1)
    (book-name-2 author-2)
    (book-name-3 author-3)
    (book-name-4 author-4)
    (book-name-5 author-5)))


(define (bookname-of-given-author bookshelf author)
  (cond
    [(empty? bookshelf)
     (error "no this author")]
    [(equal? author (second (first bookshelf)))
     (first (first bookshelf))]
    [else
     (bookname-of-given-author (rest bookshelf) author)]))


;;; Test
(bookname-of-given-author my-bookshelf 'author-1)
(bookname-of-given-author my-bookshelf 'author-4)


(define (build-author-index bookshelf author-ls)
  (cond
    [(empty? author-ls) empty]
    [else
     (cons (bookname-of-given-author my-bookshelf (first author-ls))
           (build-author-index (rest bookshelf) (rest author-ls)))]))


;;; Test
(build-author-index my-bookshelf '(author-1 author-4))
(build-author-index my-bookshelf '(author-4 author-3 author-2 author-1))
tjorchrt
  • 702
  • 4
  • 11