Tuples and Lists Lecture 3, Programmeringsteknik del A.

Slides:



Advertisements
Similar presentations
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
Advertisements

Haskell Lets review some of the Haskell concepts you have been learning on your own. The answers are included, but try it yourself first.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
0 PROGRAMMING IN HASKELL Chapter 10 - Declaring Types and Classes.
String is a synonym for the type [Char].
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
Higher-Order Functions Koen Lindström Claessen. What is a “Higher Order” Function? A function which takes another function as a parameter. Examples map.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Functional Programming Lecture 4 - More Lists Muffy Calder.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
0 PROGRAMMING IN HASKELL Some first steps Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Advanced Functional Programming 2009 Ulf Norell (lecture by Jean-Philippe Bernardy)
Chapter 9: Functional Programming in a Typed Language.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Overview of the Haskell 98 Programming Language
What is a Type? A type is a name for a collection of related values. For example, in Haskell the basic type Bool contains the two logical values: True.
Function Definition by Cases and Recursion Lecture 2, Programmeringsteknik del A.
Recursion on Lists Lecture 5, Programmeringsteknik del A.
Case Study: Route Finding Lecture 6, Programmeringsteknik del A.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: More on Functions and List Comprehensions Dr. Hyunyoung Lee.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
Functional Programming Lecture 3 - Lists Muffy Calder.
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
1 PROGRAMMING IN HASKELL Lecture 2 Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
Advanced Functional Programming 2010
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Polymorphic Functions
Functional Programming
String is a synonym for the type [Char].
Laziness and Infinite Datastructures
Conditional Expressions
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
Theory of Computation Lecture 4: Programs and Computable Functions II
Functional Programming Lecture 12 - more higher order functions
PROGRAMMING IN HASKELL
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
Higher-Order Functions
PROGRAMMING IN HASKELL
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
Computer Science 312 Haskell Lists 1.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
PROGRAMMING IN HASKELL
Fundamentals of Functional Programming
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Introduction to Computer Science
Presentation transcript:

Tuples and Lists Lecture 3, Programmeringsteknik del A.

Tuples A tuple is a value made up of several other values, its components. Examples: A point in the plane(x,y) A point in space (x,y,z) A purchase(”Dagens Lunch”, 55) The null tuple() Round brackets and commas.

Tuple Types A tuple type specifies the type of each component. Examples: (1,2) :: (Int, Int) (1.5, 2.25) :: (Float, Float) (”Dagens Lunch”, 55) :: (String, Int) () :: ()

Type Definitions We can give names to types with a type definition: type Purchase = (String, Int) Now Purchase and (String, Int) are interchangeable: dagens :: Purchase dagens = (”Dagens Lunch”, 55) All types start with a capital letter.

Pattern Matching Functions on tuples can be defined by pattern matching: name :: Purchase -> String name (s, i) = s price :: Purchase -> Int price (s, i) = i A pattern which must match the actual argument.

Standard Selector Functions fst (x, y) = x snd (x, y) = y But we cannot define: select 1 (x, y) = x select 2 (x, y) = y The type of a function’s result is not allowed to depend on the values of the arguments. What would the type of the result be?

Lists A list is also a value made up of other values, its elements. Examples: [1, 2, 3] :: [Int] [True, False, True] :: [Bool] [] :: [Float] [(”Dagens Lunch”, 55)] :: [Purchase]

Tuples vs. Lists A tuple consists of a fixed number of components, of various types. Useful, but not much to know. A list consists of any number of elements, all of the same type. Ubiquitous! Supported by a rich set of standard functions and constructions.

Lists Can Make Programs Simpler! Whenever there is a sequence of values, consider using a list.

Lists for Counting We often need to count: [1..10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [1,3..10] = [1, 3, 5, 7, 9] Example: fac :: Int -> Int fac n = product [1..n] Standard function to multiply together list elements.

List Comprehensions We often do the same thing to every element of a list: Example: doubles :: [Int] -> [Int] doubles xs = [2 * x | x <- xs] doubles [1..3][2, 4, 6] For each element x of xs... Add 2*x to the list produced.

Summing Squares with Lists n [1, 2, 3, …, n] [1, 4, 9, …, n^2] … + n^2 sumsq :: Int -> Int sumsq n = sum [i^2 | i <- [1..n]] Add up the elements of a list.

Filtering Lists A list comprehension can include a condition which elements must satisfy. Example: factors :: Int -> [Int] factors n = [i | i <- [2..n], n `mod` i == 0] factors 12[2, 3, 4, 6, 12] Include only those i which pass this test.

Using factors Counting the number of factors… numFactors :: Int -> Int numFactors n = length (factors n) Testing for a prime number… isPrime :: Int -> Bool isPrime n = factors n == [n] The number of elements in a list.

Designing List Comprehensions Ask yourself: Do I want to process a sequence of elements in turn? If so, use a list comprehension! [ | <- ] Start by writing down a framework with gaps to be filled in.

Designing List Comprehensions Ask yourself: What is the list whose elements I want to process? [ | <- [2..n]] Write it in the gap after the left arrow.

Designing List Comprehensions Ask yourself: What will I call each element? [ | i <- [2..n]] Fill the name in to the left of the arrow.

Designing List Comprehensions Ask yourself: Do I want to process every element, or make a selection from them? [ | i <- [2..n], n `mod` i == 0] Write a comma and a condition at the end of the comprehension.

Designing List Comprehensions Ask yourself: What values do I want in the result? [ i| i <- [2..n], n `mod` i == 0] Write an expression before the bar.

Lists vs. Recursion? Even problems which do not mention lists can often be solved easily by using lists internally. Haskell’s powerful list constructions often offer a simpler alternative to using recursion. But not always! Recursion is a powerful and general tool -- therefore harder to use than special purpose tools, when they are applicable.

Example: A Library Database Computerise the librarian’s card file: John Hughes borrowed The Craft of Functional Programming Simon Thompson borrowed Science Goes Boink!

Representing a Loan Card What is the information on a loan card? John Hughes borrowed The Craft of Functional Programming Borrower’s name. Book title. type Borrower = String type Book = String type Card = (Borrower, Book)

Representing the Card File What is the contents of the card file? -- a sequence of loan cards! type Database = [Card] example :: Database example = [ (”John Hughes”, ” The Craft of Functional Programming ”), (”Simon Thompson”, ”Science Goes Boink!”) ]

Querying the Database What information would we wish to extract? books :: Database -> Borrower -> [Book] borrowers :: Database -> Book -> [Borrower] borrowed :: Database -> Book -> Bool numBorrowed :: Database -> Borrower -> Int

Which Books Have I Borrowed? books example ”John Hughes” = [”The Craft of Functional Programming”] books example ”Mary Sheeran” = [] books db person = [book | (borrower, book) <- db, borrower == person] No books borrowed. Pattern matching again.

Quiz Define: numBorrowed :: Database -> Borrower -> Int

Quiz Define: numBorrowed :: Database -> Borrower -> Int numBorrowed db person = length (books db person)

Updating the Database Making and returning loans changes the contents of the database. makeLoan :: Database -> Borrower -> Book -> Database returnLoan :: Database -> Book -> Database These functions use the information in the database beforehand... … to compute the information in the database afterwards.

Making a Loan makeLoan :: Database -> Borrower -> Book -> Database makeLoan db borrower book = [(borrower, book)] ++ db Make a new card.

Returning a Loan returnLoan :: Database -> Book -> Database returnLoan db book = [(borrower, book’) | (borrower, book’) <- db, book’ /= book]

The Role of Lists Lists offer a natural way to model real world information -- sequences are everywhere! Lists are easy to manipulate thanks to Haskell’s support: a good first choice of data structure. Lists are not always efficient: candidates for replacement by faster structures later in program development.

Some Standard List Functions Haskell provides many standard functions to manipulate lists. Let’s look at a few. But first: What is the type of length?

Polymorphic Types length :: [Int] -> Int length :: [Float] -> Int length :: [Card] -> Int For any type t, length :: [t] -> Int length has many types! It is polymorphic. That is why it is useful. A type variable, which may stand for any type.

Taking and Dropping take n xsthe first n elements of xs, drop n xsall but the first n elements. Examples: take 3 (factors 12) [2, 3, 4] drop 3 (factors 12)[6, 12]

Quiz take and drop have the same (polymorphic) type. What is it?

Quiz take and drop have the same (polymorphic) type. What is it? take, drop :: Int -> [a] -> [a] Examples: Int -> [Float] -> [Float] Int -> [String] -> [String] NotInt -> [Float] -> [String] A must stand for the same type everywhere.

The Zipper Often we want to combine corresponding elements of two lists: zip [”John”, ”Simon”,”Mary” ] [”Welsh”, ”English”,”Irish”] [ (”John”,”Welsh”), (”Simon”,”English”), (”Mary”,”Irish”)]

The type of zip zip :: [a] -> [b] -> [(a, b)] Example: zip [1, 2, 3] [”a”, ”b”, ”c”] [(1,”a”), (2,”b”), (3,”c”)] Here zip :: [Int] -> [String] -> [(Int, String)]

Example: The Position of x in xs position :: a -> [a] -> Int Example: position ”b” [”a”, ”b”, ”c”]2 Idea: Mark every element with its position, and search for the one we want.

Marking Elements with Positions [”a”, ”b”, ”c”][(”a”,1), (”b”,2), (”c”,3)] zip [”a”, ”b”, ”c”] [ 1, 2, 3 ] [(”a”,1), (”b”,2), (”c”,3)] Use zip xs [1..length xs]

Searching for the Right Element Select the positions where the element we’re searching for appears: positions x xs = [pos | (x’,pos) <- zip xs [1..length xs], x’ == x] positions ”b” [”a”, ”b”, ”c”][2] A list. We want just a number.

The Complete Position Function position :: a -> [a] -> Int position x xs = head [pos | (x’,pos) <- zip xs [1..length xs], x’ == x] Select the first element from the list of positions.

Example: Calculate Path Lengths How long is the path?

Representing a Point type Point = (Float, Float) distance :: Point -> Point -> Float distance (x, y) (x’, y’) = sqrt ((x-x’)^2 + (y-y’)^2) x- and y-coordinates.

Representing a Path P Q R type Path = [Point] examplePath = [p, q, r, s] path length = distance p q + distance q r + distance r s S

Two Useful Functions init xs-- all but the last element of xs, tail xs -- all but the first element of xs. init [p, q, r, s][p, q, r] tail [p, q, r, s][q, r, s] zip …[(p,q), (q,r), (r,s)]

The pathLength Function pathLength :: Path -> Float pathLength xs = sum [distance p q | (p,q) <- zip (init xs) (tail xs)] Example: pathLength [p, q, r, s] distance p q + distance q r + distance r s

Rule of Thumb Question: Do I want to go through the elements of two lists together? Answer: Use zip!

Lessons Lists model any kind of sequence in the real world. Lists can express many kinds of repeated computation in programs. Special constructions and a rich set of polymorphic standard functions let us manipulate lists very easily indeed. Look for opportunities to use them!

Reading This lecture is largely based on Chapter 5 of the text book, up to section 5.7. Read this, and try exercises 5.11, 5.12, 5.13, and Section 5.8 summarises a number of standard functions on lists. Knowing these functions will make list programming much easier for you.