+ All Categories
Home > Documents > Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists...

Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists...

Date post: 20-Jul-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
25
Topic 4: Tuples and Lists 1
Transcript
Page 1: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

Topic 4: Tuples and Lists

1

Page 2: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

Recommended Exercises and Readings

• From Haskell: The craft of functional programming (3rd Ed.)• Exercises:

• 5.1, 5.2, 5.3, 5.15, 5.16, 5.17, 5.18, 5.19, 5.20, 5.21, 5.22, 5.23, 5.24, 5.26, and 5.28• 6.4, 6.5, 6.6, 6.7, 6.29, 6.30, 6.31, and 6.32• 7.1, 7.2, 7.3, 7.5, 7.6, 7.8, 7.9, 7.11, 7.13, 7.16, 7.18, 7.19, 7.20, 7.21, 7.23, 7.24, 7.25, 7.27, 7.28, 7.29, 7.32, 7.33, and 7.34

• Readings:• Chapters 5, 6, and 7

2

Page 3: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

Tuples

• Tuples • Combine several pieces of data into one object• Pieces may have different types

• Similar to• Struct in C/C++• Data members in a Java or C++ class• Record in Pascal• Tuple in Python

3

Page 4: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

Tuples

• Examples of tuple types

• Examples of tuple literals

• Tuples with exactly two elements are referred to as pairs

4

Page 5: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

Tuples as Parameters

• A tuple can represent a fixed‐length vector

• Write functions that compute the cross product and dot product of two vectors

5

Page 6: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

Lists

• Lists• Combine several pieces of data into one object• Every piece of data must have the same type• The number of elements is flexible

• Similar to• Arrays in C / C++ / Java• Lists in Python

• Big differences to C / C++ / Java arrays:

6

Page 7: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

Lists

• Examples of list types

• Examples of list literals

7

Page 8: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

List Functions

• head

• tail

• take

• drop

8

Page 9: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

List Functions and Operators

• length

• :

• ++

• !!

9

Page 10: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

Lists as Parameters

• Write a function named isEmptyList that returns True if the list is empty, False otherwise

• Write a function named isSingletonList that returns true if the list has length 1, False otherwise

10

Page 11: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

Recursion and Lists

• Functions that operate on lists often include recursion• Perform some task involving the first element in the list• Call the function recursively on the rest of the list• Example: Compute the sum of all the elements in a list

11

Page 12: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

Example: Run Length Encoding

• A (basic) compression technique• Works well when there are long sequences of repeated values• Used by the PCX image format

• Decode: Convert a list of pairs into a string• First element in each pair is a character• Second element in each pair is the number of times it occurs• Example: [(‘h’, 1), (‘e’, 1), (‘l’, 2), (‘o’, 1)] decodes to “hello”

12

Page 13: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

Example: Run Length Encoding

• Encode: Convert a string into a list of pairs• Example: “aaabbaaaaa” encodes as [(‘a’, 3), (‘b’, 2), (‘a’, 5)] 

13

Page 14: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

..

• Sometimes it’s inconvenient to write out all of the elements in a list• .. removes the need to do so in some situations

• Simplest case: Each element is the successor of its predecessor

• Different increment values are also possible

14

Page 15: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

Repetition Structures

• What repetition structures exist in imperative and object oriented languages? 

15

Page 16: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

Repetition Structures

• How do we repeat a task in Haskell?

16

Page 17: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

List Comprehensions

• Allow us to construct a new list from an existing list• Elements in the new list are the result of a calculation performed on the elements in the existing list

• Elements can be removed (filtered) from the old list

17

Page 18: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

List Comprehensions

• Examples

18

Page 19: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

List Comprehensions in Python

• Examples

19

Page 20: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

List Comprehensions with Multiple Lists

• List comprehensions can be performed on multiple lists

data_n = [1, 2, 3]data_m = [2, 5, 2]result = [n + m | n <‐ data_n, m <‐ data_m]putStrLn (show result)

• What is displayed by the preceding code segment?

20

Page 21: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

List Comprehensions with Multiple Lists

• How do we process parallel lists

data_n = [1, 2, 3]data_m = [2, 5, 2]

putStrLn (show result)         ‐‐ display [3, 7, 5]

21

Page 22: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

Combining Lists and Tuples

• Elements in a list can be tuples• Examples: 

• [(1, 2, 0), (3, 0, 4), (5, 7, 6)]• [("A", 65), ("a", 97)]

• A list of tuples can be constructed by zipping two lists together

• The process can be reversed with unzip

22

Page 23: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

Lists and Tuples Example

• Example:• Consider the following list of titles:

• Alice's Adventures in Wonderland• An Accountant's Nightmare• Much Ado About Nothing• Pride and Prejudice• The Adventures of Huckleberry Finn• The Matrix• The Princess Bride

• When titles are sorted, leading articles like A, An and The are normally ignored.  

• Write a ‘title sort’ function• The titles should be ordered as

• An Accountant's Nightmare• The Adventures of Huckleberry Finn• Alice's Adventures in Wonderland• The Matrix• Much Ado About Nothing• Pride and Prejudice• The Princess Bride

• Data.List contains a sort function that sorts strings into ASCII order

23

Page 24: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

Lists and Tuples Example

24

Page 25: Topic 4: Tuples and Lists - pages.cpsc.ucalgary.cabdstephe/449_F17/... · Topic 4: Tuples and Lists 1. Recommended Exercises and Readings •From Haskell: The craft of functional

Summary

• Lists and tuples make it practical to work with larger amounts of data• Tuples: Similar to a C struct or data members in a class• Lists: An ordered collection of values that all have the same type

• List and tuple elements are accessed via• Pattern matching• Functions

• Elements in a list can be processed using• Recursion• List Comprehensions

25


Recommended