6.6

Homework 7

home work!

Programming Language ISL

Due Date: Tuesday June 2, 10pm EDT

Purpose: To practice the use of pre-defined list abstractions.

Failure to comply with these expectations will result in deductions and possibly a 0 score.

For the following exercises, use ISL’s pre-defined list abstraction(s) when appropriate. If you need to define a helper for a list abstraction, define it locally.

Exercise 1 Design a function which, given a list of numbers, uses map and filter to return the squares of the even numbers in the list. Do not compute squares you will not use in the end result.

Exercise 2 Design a second version of the same function which uses foldr so that you only iterate over the elements of the list once instead of twice.

Exercise 3 Design a function map-filter with foldr which will filter a list by some predicate and then apply a function to all of the remaining elements. Then, design the initial function a third time with this new abstraction. Explicitly test both functions.

For the following exercises, refer to the data definition below. (You should use ISL’s pre-defined list abstraction(s) when appropriate for these exercises as well. And if you need to define a helper for a list abstraction, define it locally.)

(define-struct book [title authors page-count])
(define-struct article [title authors url word-count])
 
; An InfoSource is one of:
; - (make-book String [List-of String] Number)
; - (make-article String [List-of String] String Number)
; and represents either:
; - a book's name, author(s), and number of pages
; - an online article's name, author(s), url, and number of words
 
(define IS-BOOK-1
  (make-book
   "Algorithms to Live By: The Computer Science of Human Decisions"
   (list "Brian Christian" "Tom Griffiths")
   368))
 
(define IS-BOOK-2
  (make-book
   (string-append
    "Hidden Figures: "
    "The American Dream and the Untold Story of the Black Women Who Helped Win the Space Race")
   (list "Margot Lee Shetterly")
   384))
 
(define IS-ARTICLE-1
  (make-article
   "Teach Yourself Programming in Ten Years"
   (list "Peter Norvig")
   "http://norvig.com/21-days.html"
   1790))
 
(define IS-ARTICLE-2
  (make-article
   "Is There a Fix for Impostor Syndrome?"
   (list "Elizabeth Churchill")
  "https://interactions.acm.org/archive/view/may-june-2018/is-there-a-fix-for-impostor-syndrome"
   1655))
 
; infosource-temp : InfoSource -> ?
(define (infosource-temp i)
  (...
   (cond [(book? i) (... (book-title i)
                         (los-temp (book-authors i))
                         (book-page-count i) ...)]
         [(article? i) (... (article-title i)
                            (los-temp (article-authors i))
                            (article-url i)
                            (article-word-count i) ...)])))

Exercise 4 Design the function only-articles that accepts a list of information sources, and produces a list of only the online articles.

Exercise 5 Design the function all-authors that returns a single list of the all the authors in a list of info sources. You do not need to remove duplicates, if any exist.

Exercise 6 Design the function num-books that counts the number of books in a list of info sources.

Exercise 7 Design the function multi-author? that determines if a supplied info source list contains at least one source with more than one author.

Exercise 8 Design the function total-words that computes the total number of words in a list of info sources. Assume that a book page has 250 words.

Exercise 9 Design the function secure-articles? that returns true if all articles in a list of info sources are served from secure URLs (i.e., start with "https").

Exercise 10 Design the function natnum->books that returns a list of books according to the following pattern...
(list (make-book "0" (list "0") 1)
      (make-book "1" (list "0" "1") 2)
      (make-book "2" (list "0" "1" "2") 3) ...)
The number input to the function should determine the length of the output list.