Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 4.1

Similar documents
More About Recursive Data Types

CS 5010 Program Design Paradigms Lesson 6.1

Lists of Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 5.3

ormap, andmap, and filter

Case Study: Undefined Variables

Case Study: Free Variables

Trees. CS 5010 Program Design Paradigms Bootcamp Lesson 5.1

Lists of Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 6.5

Solving Your Problem by Generalization

Contracts, Purpose Statements, Examples and Tests

The Data Design Recipe

Observer Templates. CS 5010 Program Design Paradigms Lesson 1.4

Rewriting your function using map and foldr

From Templates to Folds

Halting Measures and Termination Arguments

Module 5: Lists. Readings: HtDP, Sections 9, 10.

How to Design Programs

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Working with recursion

Using Inheritance to Share Implementations

Lists. Readings: HtDP, sections 9 and 10. Avoid 10.3 (uses draw.ss). CS 135 Winter : Lists 1

foldr CS 5010 Program Design Paradigms Lesson 5.4

Working with images and scenes

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Repetition Through Recursion

Types of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion

CS115 - Module 10 - General Trees

Basics of Inheritance

Module 8: Binary trees

Types of recursion. Readings: none. In this module: a glimpse of non-structural recursion. CS 135 Winter : Types of recursion 1

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

Module 9: Binary trees

Trees. Binary arithmetic expressions. Visualizing binary arithmetic expressions. ((2 6) + (5 2))/(5 3) can be defined in terms of two smaller

Module 10: General trees

The purpose of this lesson is to familiarize you with the basics of Racket (a dialect of Scheme). You will learn about

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

6.001 Notes: Section 15.1

Binary Search. CS 5010 Program Design Paradigms Bootcamp Lesson 8.2

Trees. Readings: HtDP, sections 14, 15, 16.

Generative and accumulative recursion. What is generative recursion? Example revisited: GCD. Readings: Sections 25, 26, 27, 30, 31

Module 9: Trees. If you have not already, make sure you. Read How to Design Programs Sections 14, 15, CS 115 Module 9: Trees

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

Trees. Example: Binary expression trees. Example: Evolution trees. Readings: HtDP, sections 14, 15, 16.

6.001 Notes: Section 8.1

Local definitions and lexical scope

Local definitions and lexical scope. Local definitions. Motivating local definitions. s(s a)(s b)(s c), where s = (a + b + c)/2.

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Lecture 3: Recursion; Structural Induction

MITOCW watch?v=kz7jjltq9r4

Laboratory: Recursion Basics

Typed Racket: Racket with Static Types

Homework 8: Matrices Due: 11:59 PM, Oct 30, 2018

Unit E Step-by-Step: Programming with Python

Model-View-Controller Architecture

+ Abstract Data Types

Homework 6: Higher-Order Procedures Due: 11:59 PM, Oct 16, 2018

Intro. Scheme Basics. scm> 5 5. scm>

Trees. CS 5010 Program Design Paradigms Lesson 6.2

CSCC24 Functional Programming Scheme Part 2


Module 8: Local and functional abstraction

Lecture: Functional Programming

Doing it in Java. CS 5010 Program Design Paradigms Bootcamp Lesson 5.4

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

Programming Paradigms

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

CMSC 131A, Midterm 1 (Practice) SOLUTION. Fall 2017

Structure and Interpretation of Computer Programs

CS2500 Exam 2 Fall 2011

CS1102: Macros and Recursion

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

Programming Languages and Techniques (CIS120)

Scheme: Expressions & Procedures

CSE413 Midterm. Question Max Points Total 100

CS 61B, Spring 1999 MT3 Professor M. Clancy

Discussion 4. Data Abstraction and Sequences

Random Testing in 321

Recursively Enumerable Languages, Turing Machines, and Decidability

Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5

CSU211 Exam 2 Fall 2007

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

CS1102: Adding Error Checking to Macros

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

CS 1110 Final, December 8th, Question Points Score Total: 100

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

About the Final. Saturday, 7-10pm in Science Center 101. Closed book, closed notes. Not on the final: graphics, file I/O, vim, unix

A Small Interpreted Language

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

Contracts and Abstraction Casts Checking a Type Interfaces

CS 11 Ocaml track: lecture 4. Today: modules

CPSC 311, 2010W1 Midterm Exam #2

CS W3134: Data Structures in Java

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

MetacircularScheme! (a variant of lisp)

CSCI0170. Today s topics. Predicates Natural Number recursion Recursion Diagrams List recursion A first glance at the design recipe

PROBLEM SOLVING 11. July 24, 2012

Transcription:

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

Module 04 Basic Principles Tools and Techniques Object-Oriented Programming Designing Data Computing with Lists Interfaces and Classes Designing Functions Designing Systems Computing with Trees and Graphs Designing with Invariants Thinking about Efficiency Inheritance Objects with Mutable State Efficiency, Part 2 2

Module Introduction This week, we will learn about list data, which is a natural representation for sequences. We will learn about the arithmetic of lists the observer template for list data lists of structures 3

Learning Objectives for Lesson 4.1 At the end of this lesson, you should be able to: Write down a data definition for information represented as a list Notate lists using constructor, list, and write notations. Explain how lists are represented as singly-linked data structures, and how cons, first, and rest work on these structures Calculate with the basic operations on lists: cons, first, and rest. So let s get started... 4

Sequence Information a phone book, which is a sequence of listings a presentation, which is a sequence of slides In Racket, these can be represented as lists. for some applications, there are more efficient representations, but we ll start with lists. 5

Lists: A Handy Representation for Sequences Sequences of data items arise so often that Racket has a standard way of representing them. Sequence information in Racket is most often represented by lists. These are implemented as singly-linked lists, which you should have learned about in your data structures course. 6

Example Data Definition for Sequence ;; DATA DEFINITION: Data There are many ways to represent a sequence of numbers. Here we ve chosen to represent a sequence as a singly-linked list. ;; A NumberSeq is represented as a list of Number. ;; CONSTRUCTOR TEMPLATES: ;; empty we -- have the two constructor empty sequence templates. ;; (cons n ns) ;; WHERE: ;; n is a Number -- the first number ;; in the sequence ;; ns is a NumberSeq -- the rest of the ;; numbers in the sequence We must give an interpretation for each possible NumberSeq. There are two ways to build a NumberSeq, so We ll deal with the observer template in the next lesson. empty and cons are built into Racket. We don t need any define-structures for them 7

Examples of NumberSeq: empty (cons 11 empty) (cons 22 (cons 11 empty)) (cons 33 (cons 22 (cons 11 empty))) A NumberSeq is one of: -- empty -- (cons Number NumberSeq) (cons 33 empty) Here are some examples of NumberSeqs. empty is a NumberSeq by the data definition. (cons 11 empty) is a NumberSeq because 11 is a number and empty is a NumberSeq. (cons 22 (cons 11 empty)) is a NumberSeq because 22 is a number and (cons 11 empty) is a NumberSeq. 8 And so on.

DigitSequence A Digit is one of "0" "1" "2"... "9" A DigitSequence (Dseq) is represented as a listof Digit. CONSTRUCTOR TEMPLATES: -- empty -- (cons Digit DSeq) Let's do it again, this time with digits. We define a Digit to be one of the strings "0", "1", etc., through "9". A DigitSequence (DSeq) is either empty or the cons of a Digit and a DSeq. 9

Examples of DSeqs empty (cons "3" empty) (cons "2" (cons "3" empty)) (cons "4" (cons "2" (cons "3" empty))) These are not DSeqs: (cons 4 (cons "2" (cons "3" empty))) (cons (cons "3" empty) (cons "2" (cons "3" empty))) A DigitSequence (DSeq) is one of: -- empty -- (cons Digit DSeq) Can you explain why each of the first 4 examples are DSeq s, according to the data definition? Can you explain why the last two are not Dseq s? 10

These data definitions are selfreferential A NumberSeq is one of: -- empty -- (cons Number NumberSeq) The data definition for NumberSeqs contains something we haven't seen before: self-reference. The second constructor uses NumberSeq, even though we haven't finished defining NumberSeqs yet. That's what we mean by self-reference. In normal definitions, this would be a problem: you wouldn t like a dictionary that did this. But self-reference the way we've used it is OK. We've seen in the examples how this works: once you have something that you know is a NumberSeq, you can do a cons on it to build another NumberSeq. Since that's a NumberSeq, you can use it to build still another NumberSeq. We also call this a recursive data definition. 11

This one is self-referential, too A Digit is one of "0" "1" "2"... "9" A DigitSequence (DSeq) is one of: -- empty -- (cons Digit DSeq) 12

How Lists Represent Sequences If X is some data definition, we define a list of X's as either empty or the cons of an X and a list of X's. So a list of sardines is either empty or the cons of a sardine and a list of sardines. The interpretation is always "a sequence of X's". empty represents a sequence with no elements (cons x lst) represents a sequence whose first element is x and whose other elements are represented by lst. If we had some information that we wanted to represent as a list of X's (say a list of people), we would have to specify the order in which the X's appear (say "in increasing order of height"), or else say in any order. 13

The constructor template for list data CONSTRUCTOR TEMPLATES for XList -- empty interp: a sequence of X's with no elements -- (cons X XList) interp: (cons x xs) represents a sequence of X's whose first element is x and whose other elements are represented by xs. We ve written XList here, but there is no such thing as an XList. There are only NumberLists, or DigitLists, or SardineLists. We ll write XList when we mean a list of X s for some X, but we don t care which it is. We use xs (pronounced "ex's") as the plural of "x". 14

Collection Information Sometimes we are interested in representing a set, which may not have a built-in notion of order a space-invaders game with many invaders a book-store inventory with many books You can use lists to represent these as well. You will need to specify the order (or lack thereof) This will be part of the data definition 15

Example Data Definition for Collection Information ;; 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 Note that here we've put the constraints on order and multiplicity right in the data definition. bs is a BookStatus inv is an Inventory and (bookstatus-isbn bs) is less than the ISBN of any book in inv. 16

List Notation There are several ways to write down lists. We've been using the constructor notation, since that is the most important one for use in data definitions. The second most important notation we will use is list notation. In Racket, this is the standard notation in the Intermediate Student Language. Internally, lists are represented as singly-linked lists. On output, lists may be notated in write notation. 17

Examples of List Notation Constructor notation: List notation: (cons 11 (cons 22 (cons 33 empty)))) (list 11 22 33) Internal representation: 11 22 33 write-style (output only): (11 22 33) 18

Implementation of cons (cons 11 lst) lst Now that we've seen the internal representation of lists, we can see how cons creates a new list: it simply adds a new node to the front of the list. This operation takes a short, fixed amount of time. 11 22 33 lst = (list 22 33) (cons 11 lst) = (list 11 22 33) 19

Operations on Lists empty? : XList -> Boolean Given a list of X's, returns true iff the list is empty Racket provides 3 functions for inspecting lists and taking them apart. These are empty?, first, and rest. The predicate empty? returns true if and only if the list is empty. 20

Operations on Lists first : XList -> X GIVEN: a non-empty list of X s RETURNS: the first element in the list. When we write down the template for lists, we will see that when we call first, its argument will always be nonempty. 21

Operations on Lists rest : XList -> XList GIVEN: a non-empty list of X s. RETURNS: the list of all its elements except the first When we write down the template for lists, we will see that when we call rest, its argument will always be nonempty. 22

Examples (empty? empty) = true (empty? (cons 11 empty)) = false (empty? (cons 22 (cons 11 empty))) = false (first (cons 11 empty)) = 11 (rest (cons 11 empty)) = empty (first (cons 22 (cons 11 empty))) = 22 (rest (cons 22 (cons 11 empty))) = (cons 11 empty) (first empty) Error! (Precondition failed) (rest empty) Error! (Precondition failed) 23

Implementation of first and rest first and rest simply follow a pointer in the singlylinked data structure. lst2 (rest lst2) (first lst2) 11 22 33 lst2 = (list 11 22 33) (first lst2) = 11 (rest lst2) = (list 22 33) 24

Properties of cons, first, and rest (first (cons v l)) = v (rest (cons v l)) = l If l is non-empty, then (cons (first l) (rest l)) = l Here are some useful facts about first, rest, and cons. Can you see why they are true? These facts tell us that if we want to build a list whose first is x and whose rest is lst, we can do this by writing (cons x lst). 25

Summary At this point, you should be able to: Write down a data definition for information represented as a list Notate lists using constructor, list, and write notations. Explain how lists are represented as singly-linked data structures, and how cons, first, and rest work on these structures Calculate with the basic operations on lists: cons, first, and rest. 26

Next Steps If you have questions about this lesson, ask them on the Discussion Board Do Guided Practices 4.1 and 4.2 Go on to the next lesson 27