topic 4: tuples and lists -...

25
Topic 4: Tuples and Lists 1

Upload: hadan

Post on 16-Apr-2018

223 views

Category:

Documents


2 download

TRANSCRIPT

Topic 4: Tuples and Lists

1

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

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

Tuples

• Examples of tuple types

• Examples of tuple literals

• Tuples with exactly two elements are referred to as pairs

4

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

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

Lists

• Examples of list types

• Examples of list literals

7

List Functions

• head

• tail

• take

• drop

8

List Functions and Operators

• length

• :

• ++

• !!

9

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

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

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

Example: Run Length Encoding

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

13

..

• 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

Repetition Structures

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

15

Repetition Structures

• How do we repeat a task in Haskell?

16

List Comprehensions

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

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

17

List Comprehensions

• Examples

18

List Comprehensions in Python

• Examples

19

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

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

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

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

Lists and Tuples Example

24

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