7.8

Homework 7

home work!

Programming Language ISL

Due Date: Friday October 30, 6pm.

Purpose: To practice designing functions with existing list abstractions, and to practice designing new list abstractions.

Expectations

List Data Definition:

; A [List-of X] is one of:
; - '()
; - (cons X [List-of X])
; 
; Interpretation: A list of items, where every item is an X.
 
; list-template : [List-of X] -> ?
(define (list-template l)
  (cond
    [(empty? l) ...]
    [(cons? l) (... (first l) ...
                    (list-template (rest l)) ...)]))
Finger Exercises

Exercise 1 From HTDP, 250 252 254 255 265 266 267 269 271 272 (use foldr for 272)

Graded Exercises:

You work at a media agency, and your job is to help clients manage their online presence. But, there are so many platforms these days: Twitter, Facebook, and even Medium, for the few people who write in long form.

Exercise 2 Design a data definition that holds information about a single social media item, which may be a Twitter tweet, Facebook post, or Medium story. For a tweet, the data definition should hold the text of the tweet (at most 280 characters), the number of likes, and the number of retweets. For a Facebook post, the data definition should hold the text of the post, and the number of likes. For a Medium story, the data definition should hold the text of the story and the number of page views.

Exercise 3 Design a function that consumes a list of social media items and produces only the tweets in that list that have at least one retweet.

You must use list abstractions to solve this problem.

Exercise 4 Design a function that takes a list of social media items and produces only the items that have never been viewed, shared, liked, or retweeted. (These items were clearly utter failures!)

You must use list abstractions to solve this problem.

Exercise 5 Clients want to know how much engagement they are getting on social media. Design a function that takes a list of social media items and produces a single number that is the sum of the likes, retweets, and page views in that list.

You know that it does not make sense to add tweets and page views. Sadly, not all your celebrity clients understand types and signatures as well as you do.

You must use list abstractions to solve this problem.

Exercise 6 You know that it is critical to "cross post" anything you share on one platform to another platform, to maximize engagement.

You have several older clients who use Facebook exclusively, and it is your job to copy their Facebook posts to Twitter and Medium. Similarly, you have clients that use Twitter exclusively, and other clients that use Medium exclusively. You can write a single function to handle all of these cases.

Design a function called crosspost that consumes a list of social media items, and produces a list that has two items for each item in the given list. Each given Facebook post must produce a Tweet with the same text (limited to 280 characters) and zero retweets, and a Medium story with the same text and zero page views. Follow the same principle for tweets and stories in the original list, keeping in mind that tweets are character-limited.

You must use the list template to solve this problem.

Hint: See the append function, which is built in to ISL.

Exercise 7 Design a function called append-apply-to-all that consumes a function f and a list l. Your function will expect f to produce a list of results when applied to an element of l. The append-apply-to-all will append these lists together, and produce a single list as its result.

For example, suppose (f x) produces (list a1 a2 a3) and (f y) produces (list b1 b2). In this case, (append-apply-to-all f (list x y)) must produce (list a1 a2 a3 b1 b2).

You may solve this problem using any technique you like.

Exercise 8 Solve the crosspost problem again, using append-apply-to-all as a helper function. Do not update your existing crosspost solution, but create a new version called crosspost/v2.

You must use list abstractions to solve this problem.

Exercise 9 Let us assume that we are working with social media items that are ordered by date: the first item in the list was the first post that was made, and the last item is the most recent post.

Design a function called items-since-tweet that consumes a list of social media items (in order), and the text of a single tweet, and produces all the social media items that were made after the given tweet, and include the given tweet itself.

You must use the list template to solve this problem.

Exercise 10 Design a function called items-since-10-likes that consumes a list of social media items (in order), and produces all the social media posts that were made after the first Facebook post that received 10 or more likes. You must include that Facebook post itself in the produced list.

You must use the list template to solve this problem.

Exercise 11 Design a function called suffix-from-2500 that consumes a list of numbers, and produces the suffix of that list that begins from the first 2500 that occurs in the given list. The produced list must include the first 2500.

You must use the list template to solve this problem.

Exercise 12 Design a function – a list abstraction – that abstracts the common pattern that appears in suffix-from-2500, items-after-10-likes, and posts-after-tweet.

You must use the list template to solve this problem.

Exercise 13 Using the list abstraction that you designed in the previous problem, write items-since-tweet/v2, items-since-10-likes/v2, and suffix-from-2500/v2.

You must use list abstractions to solve this problem.