Lists of Structures. CS 5010 Program Design Paradigms Bootcamp Lesson 4.3

Similar documents
Teaching Citations as a Multi-Functional Approach to Archives Instruction

How to find out information about the report

PROFESSOR: Well, last time we talked about compound data, and there were two main points to that business.

d. Could you represent the profit for n copies in other different ways?

INDE/TC 455: User Interface Design. Module 5.4 Phase 4 Task Analysis, System Maps, & Screen Designs

Octaves and the Major-Minor Tonal System *

Custom Coursepack Centre INFORMATION PACKAGE (2011)

BARC Tips for Tiny Libraries

Octaves and the Major-Minor Tonal System

Create and publish your Aspire reading list

VRAcore:

Modules Multimedia Aligned with Research Assignment

World of Music: A Classroom and Home Musical Environment

Format and Style of a MLA Paper

MBS ARC (Textbook) Manual

Pitch: Sharp, Flat, and Natural Notes

Lightning Source & Lulu Online Print-on-Demand/eBook Publishers (Overview & Comparison)

USING ENDNOTE X2 SOFTWARE

Integrating Your Sources: Quotations, Paraphrasing, and Summarizing

Writing Better Lyrics PDF

Deltasoft Services M A N U A L LIBRARY MANAGEMENT. 1 P a g e SCHOOL MANAGEMENT SYSTEMS. Deltasoft. Services. User Manual. Aug 2013

OpenStax-CNX module: m Time Signature * Catherine Schmidt-Jones

Designing a Deductive Foundation System

Migratory Patterns in IRs: CONTENTdm, Digital Commons and Flying the Coop

Heuristic Search & Local Search

OpenStax-CNX module: m Clef * Catherine Schmidt-Jones. Treble Clef. Figure 1

MLA Citations Practice

MA 15910, Lesson 5, Algebra part of text, Sections 2.3, 2.4, and 7.5 Solving Applied Problems

The Circle of Fifths *

WorldCat Discovery User Guide 2018

Austin Brothers Publishing Process

ATTIC BOOKS. books & curiosities

Editing EndNote Output Styles Rosemary Rodd 5/23/ Configuring EndNote at the University Managed Cluster sites

MLA ANNOTATED BIBLIOGRAPHIES. For use in your Revolutionary Song projects

PROFESSOR: I'd like to welcome you to this course on computer science. Actually, that's a terrible way to start.

Um... yes, I know that. (laugh) You don't need to introduce yourself!

CS 3 Midterm 1 Review

The Non-Designer's Design Book PDF

2.00 x x 1.0

Composing with Courage

Self Publishing a (D) Book

Harmonic Generation based on Harmonicity Weightings

Feature-Specific Profiling

Piano Teacher Program

Works Cited Information Books. Author(s): Title: City of Publication: Publishing company: Year of Publication: Medium of Publication:

Shelley McNamara.

2018 RICHELE & LINDSEY PRODUCTIONS, LLC TALKINGMOM2MOM.COM

TOPIC: 5 WINNING WAYS TO MARKET TO BOOKSTORES AND LIBRARIES. TOPIC: Helping Each Other Achieve and Succeed PRESENTER: MIMI LE IBPA PROJECT MANAGER

StickIt! VGA Manual. How to install and use your new StickIt! VGA module

Organizing Articles into Volumes and Issues Considerations for Special Issues

Editing Your Reading List

1) Open EndNote. When asked, choose an existing library or Create a New Library.

Summer Reading for Rising 5 th Graders Due: 1 st day of school.

Getting Started with Cataloging. A Self-Paced Lesson for Library Staff

Overview. Project Shutdown Schedule

Store Inventory Instruction Guide

DICOM Correction Item

Rapping Manual Table of Contents

Before the. Federal Communications Commission. Washington, DC

INTERMEDIATE PROGRAMMING LESSON

Modern Calligraphy: Everything You Need To Know To Get Started In Script Calligraphy PDF

Managing a Time Clock Station

Commonly Misused Words

Guide To Publishing Your. CREATESPACE Book. Sarco2000 Fiverr Book Designer

GRADE 11 NOVEMBER 2013 ENGLISH FIRST ADDITIONAL LANGUAGE P3

ABBOTT AND COSTELLO By Jonathan Mayer

Table of Contents. iii

Printed Documentation

Eagle Business Software

AP MUSIC THEORY 2011 SCORING GUIDELINES

What was once old... two recent initiatives at HKU

Instructional Materials Procedures

_FM 7/22/09 10:10 AM Page 1 COLLABORATING. with SharePoint. Carey Cole

WEED EM AND WEEP? Tips for Weeding Library Collections

(12) United States Patent (10) Patent No.: US 6,628,712 B1

The Elements Of Style: The Classic Writing Style Guide By William Strunk, E. B. White

Library Acquisition Patterns Preliminary Findings

SUBJECT: YEAR: Half Term:

-SQA-SCOTTISH QUALIFICATIONS AUTHORITY. Hanover House 24 Douglas Street GLASGOW G2 7NQ NATIONAL CERTIFICATE MODULE DESCRIPTOR

Corinne: I m thinking of a number between 220 and 20. What s my number? Benjamin: Is it 25?

Rental Setup and Serialized Rentals

Braille Module 67. Literary Braille Book Format Continued LOC Literary Lesson 19, Sections

I. PREREQUISITE For information regarding prerequisites for this course, please refer to the Academic Course Catalog.

Borrowing Resources through Interlibrary Loan: Illiad

Workbooks for undergraduate counterpoint 1-4

Parent Guide. Carly Seifert. Welcome to Busy Kids Do Piano!

Outline. Why do we classify? Audio Classification

Chapters Page #s Due Date Comments

DECLUTTER YOUR COLLECTION

Applying effects including adjusting volume and fade in and out

A More-Product-Less-Process Approach to Cataloging Recordings

LESSON PLAN. By Carl L. Williams

MANAGERS REFERENCE GUIDE FOR

GlobeEdit. Worldwide Distribution

Abstract. Justification. 6JSC/ALA/45 30 July 2015 page 1 of 26

Instructor (Mehran Sahami):

Creating Color Combos

Cereal Box Book Report

PRODUCTION OF INFORMATION MATERIALS WHY PUBBLISHING PARTNERS IN THE BOOK TRADE FUNCTIONS OF PUBLISHING

Proofed Paper: ntp Mon Jan 30 23:05:28 EST 2017

Transcription:

Lists of Structures CS 5010 Program Design Paradigms Bootcamp Lesson 4.3 Mitchell Wand, 2012-2017 This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. 1

Introduction Lists of structures occur all the time Programming with these is no different: write down the data definition, including interpretation and template Follow the Recipe! 2

Learning Objectives At the end of this lesson you should be able to: write down a template for lists of compound data use the template to write simple functions on lists of compound data 3

Programming with lists of structures Programming with lists of structures is no different from programming with lists of scalars, except that we make one small change in the recipe for templates 4

Example: modeling a bookstore Let's imagine a program to help manage a bookstore. Let s build a simple model of the inventory of a bookstore. 5

Step 1: Data Design First, we ll give data definitions for the various quantities we need to represent: 6

Preliminary Data Definitions ;; An Author is represented as a String (any string will do) ;; A Title is represented as a String (any string will do) ;; An International Standard Book Number (ISBN) is represented ;; as a positive integer (PosInt). ;; A DollarAmount is represented as an integer. ;; INTERP: the amount in USD*100. ;; eg: the integer 3679 represents the dollar amount $36.79 ;; A DollarAmount may be negative. We might refine this definition later, eg keep track of FirstName, LastName, etc. Actually, an ISBN is a sequence of exactly 13 digits, divided into four fields (see https://en.wikipedia.org/wiki/international_standard_book_number). We don't need to represent all this information, so we will simply represent it as a PosInt. 7

BookStatus ;; A BookStatus is represented as ;; (book-status isbn author title cost price on-hand) ;; INTERP: ;; isbn : ISBN -- the ISBN of the book ;; author : Author -- the book's author ;; title : Title -- the book's title ;; cost : DollarAmount -- the wholesale cost of the book (how much ;; the bookstore paid for each copy of the ;; book ;; price : DollarAmount -- the price of the book (how much the ;; bookstore charges a customer for the ;; book) ;; on-hand: NonNegInt -- the number of copies of the book that are ;; on hand in the bookstore) Note that we are not modelling a Book (that s something that exists on a shelf somewhere ). We are modelling the status of all copies of this book. 8

BookStatus (cont d) ;; IMPLEMENTATION: (define-struct book-status (isbn author title cost price on-hand)) ;; CONSTRUCTOR TEMPLATE: ;; (make-book-status ISBN Author Title DollarAmount DollarAmount NonNegInt) ;; OBSERVER TEMPLATE: ;; book-status-fn : BookStatus ->?? (define (book-status-fn b) (... (book-status-isbn b) (book-status-author b) (book-status-title b) (book-status-cost b) (book-status-price b) (book-status-on-hand b))) 9

Inventory ;; An Inventory is represented as a list of ;; BookStatus, in increasing ISBN order, with at ;; most one entry per ISBN. ;; CONSTRUCTOR TEMPLATES: ;; empty ;; (cons bs inv) ;; -- WHERE ;; bs is a BookStatus ;; inv is an Inventory ;; and ;; (bookstatus-isbn bs) is less than the ISBN of ;; any book in inv. 10

Inventory (cont d) ;; OBSERVER TEMPLATE: ;; inv-fn : Inventory ->?? (define (inv-fn inv) (cond [(empty? inv)...] [else (... (first inv) (inv-fn (rest inv)))])) 11

Inventory (cont d) (define (inv-fn inv) (cond [(empty? inv)...] [else (... (book-status-fn (first inv)) (inv-fn (rest inv)))])) Since (first inv) is a BookStatus, it would also be OK to write the observer template like this. These templates are there to serve as a guide for you, so we are going to try not to be too picky about them. But you must put the recursive call to inv-fn in your observer template. 12

Remember: The Shape of the Program Follows the Shape of the Data Inventory is-component-of inv-fn calls BookStatus bookstatus-fn Data Hierarchy (a non-empty inventory contains a BookStatus and another Inventory) Call Tree (inv-fn calls itself and book-status-fn) 13

Example function: inventory-authors ;; inventory-authors : Inventory -> AuthorList ;; GIVEN: An Inventory ;; RETURNS: A list of the all the authors of the books in the ;; inventory. Repetitions are allowed. Books with no copies in stock ;; are included. The authors may appear in any order. ;; EXAMPLE: (inventory-authors inv1) = (list "Felleisen" "Wand" "Shakespeare" "Shakespeare") ;; STRATEGY: Use observer template for Inventory (define (inventory-authors inv) (cond [(empty? inv) empty] [else (cons (book-status-author (first inv)) (inventory-authors (rest inv)))])) 14

An Inventory but which inventory? So far we've decided how to represent an inventory. But what store is it the inventory of? And what date does it represent? 15

BookstoreState ;; A Date is represented as a... ;; A BookstoreState is represented as a (bookstore-state date stock) ;; INTERP: ;; date : Date -- the date we are modelling ;; stock : Inventory -- the inventory of the bookstore as of 9am ET on ;; the given date. ;; IMPLEMENTATION: (define-struct bookstore-state (date stock)) ;; CONSTRUCTOR TEMPLATE ;; (make-bookstore-state Date Inventory) ;; OBSERVER TEMPLATE ;; state-fn : BookstoreState ->?? (define (state-fn bss) (... (bookstore-state-date bss) (bookstore-state-stock bss))) Now that we have a history of the inventory, we can do more things, like track the value of the inventory over time, compare the sales of some book over some time period, etc., etc. 16

Module Summary: Self-Referential or Recursive Information Represent arbitrary-sized information using a self-referential (or recursive) data definition. Self-reference in the data definition leads to self-reference in the observer template. Self-reference in the observer template leads to self-reference in the code. Writing functions on this kind of data is easy: just Follow The Recipe! But get the template right! 17

Summary At the end of this lesson you should be able to: write down a template for lists of compound data use the template to write simple functions on lists of compound data The Guided Practices will give you some exercise in doing this. 18

Next Steps Study 04-2-books.rkt in the Examples file If you have questions about this lesson, ask them on the Discussion Board Do Guided Practice 4.4 Go on to the next lesson 19