Functional Programming Lecture 3 - Lists Muffy Calder.

Slides:



Advertisements
Similar presentations
Formal Models of Computation Part II The Logic Model
Advertisements

Introduction A function is called higher-order if it takes a function as an argument or returns a function as a result. twice :: (a  a)  a  a twice.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
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.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Nested definitions Lists Lecture notes, exercises, etc., can.
0 LECTURE 5 LIST COMPREHENSIONS Graham Hutton University of Nottingham.
Map and Fold Building Powerful Abstractions. Hello. I’m Zach, one of Sorin’s students.
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.
Advanced Programming Handout 4. Introductions  Me: Benjamin C. Pierce (known as Benjamin, or, if you prefer, Dr. Pierce, but not Ben or Professor) 
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
Haskell Chapter 3, Part I. Pattern Matching  Pattern matching with tuples  Pattern matching with list comprehensions  As-patterns.
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.
Functional Programming in Haskell Motivation through Concrete Examples Adapted from Lectures by Simon Thompson.
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)
0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.
Modeling with Haskell Scientific Seminar 03/04 Gerhard Navratil.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
1 Functional Programming Lecture 6 - Algebraic Data Types.
Overview of the Haskell 98 Programming Language
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
Recursion on Lists Lecture 5, Programmeringsteknik del A.
Kyung-Goo Doh Hanyang University - ERICAComputer Science & Engineering Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
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,
Recursion Higher Order Functions CSCE 314 Spring 2016.
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
Lecture 16: Advanced Topic: Functional Programming CS5363 Compiler and Programming Languages.
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)
© 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.
Lecture 14: Advanced Topic: Functional Programming
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
Functional Programming
Types CSCE 314 Spring 2016.
Programming Languages and Compilers (CS 421)
Theory of Computation Lecture 4: Programs and Computable Functions II
Functional Programming Lecture 12 - more higher order functions
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Higher-Order Functions
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
An aggregation mechanism
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Higher Order Functions
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSE 3302 Programming Languages
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
Presentation transcript:

Functional Programming Lecture 3 - Lists Muffy Calder

The List Types A list can have any number of elements All the elements must have the same type If the elements have type a then the list has type [a] You can put any type of value in a list –For every type a there is a type [a] x1,…,xn::a a :: Type list type rules [x1,…,xn]::[a] [a] :: Type

List Notation – finite, simple lists Put square brackets around the list and separate the elements with commas All elements must have the same type –[1, 2, 3, 4] :: [Int] –[ ] :: [a] for every type a Can have expressions in the list –[13, 2+2, 5*x] => [13, 4, 150] Can have a list of lists [[1,2], [3,7,1], [ ], [900]] :: [[Int]] a list of lists!

Strings A string is just a list of characters. [‘c‘, ‘a‘, ‘t‘] is same as “cat”. The Standard Haskell library contains type String = [Char] (A type synonym.) “cat” :: String [‘c‘, ‘a‘, ‘t‘] ::String What is the type of [“cat”,”dog”]?

More List Notation Give the head and the tail of the list, as x:xs –x is the head –xs is the tail –when x :: a and xs :: [a], then x:xs :: [a] –: is pronounced cons –example [1,2,3,4] == 1:(2:(3:(4:[]))) == 1:(2:(3:[4])) x:: a xs :: [a] x:xs ::[a]

More List Notation Taking lists apart: head and tail head gives first element of a nonempty list head :: [a] -> a –head [a,b,c,d] = head (a : [b,c,d]) = a –it is an error to apply head to [] tail removes first element from a nonempty list tail :: [a] -> [a] –tail [a,b,c,d] = tail (a : [b,c,d]) = [b,c,d]

Head and Tail head (x:xs) = x -- head.1 tail (x:xs) = xs -- tail.1 Why no head.0? xs :: [a] xs ::[a] tail xs :: [a] head xs :: a (sometimes!) Theorem: For every nonempty list xs. ((head xs):(tail xs)) == xs Do you believe this? Could you prove it?

Sequences You can use “..” in the “usual” (!) way. [1..5] => [1,2,3,4,5] [1..n] => [1, 2, 3, …, n] [10,13..20] => [10, 13, 16, 19] [a,b..c] => [a, a+(b-a), a+2*(b-a), …] ['a'.. 'z'] => "abcdefghijklmnopqrstuvwxyz” ['a', 'c'.. 'z'] => "acegikmoqsuwy" :: [Char] [‘0‘.. ‘9‘] => “ ”

The length of a list length :: [a] -> Int length [] = 0 (length.0) length (x:xs) = 1 + length xs (length.1) Defining a function this way uses: pattern matching and primitive recursion. length [3,8,1] => 3 length [ ] => 0 length “hello” => 5 length [1..n] => n length [1..] => infinite loop

Mapping One of the most important list operations Apply a function to each element of a list map :: (a->b) -> [a] -> [b] map sqrt [1,4,9,16] => [1.0, 2.0, 3.0, 4.0]

The type of map map :: (a->b) -> [a] -> [b] map takes two arguments a function of type a->b a list of type [a] and returns a result a list of type [b] f:: a -> b xs::[a] map f xs :: [b] The function has to take an element of the argument and produce an element of the result map f [] = [] (map.0) map f (x:xs) = (f x) : (map f xs) (map.1)

Indexing a list The !! operator lets you access a list element by index (the indices start from 0) [x 0, x 1, x 2, … x n ] !! i => x i

List Concatenation (++) Takes two lists and joins them together [1,2,3] ++ [9,8,7] => [1,2,3,9,8,7] All the elements of the result list must have the same type; therefore both argument lists must have the same type (++) :: [a] -> [a] -> [a] [] ++ ys = ys(++.0) (x:xs) ++ ys = x:(xs++ys) (++.1)

Examples Define a function which concatenates a list of strings. E.g. concat [“make”, “into”, “bigstring”] => “makeintobigstring” concat :: [String] -> String concat [] = [] concat x:xs = x ++ concat xs a better version concat2 [] = [] concat2 [x] = x concat2 x:xs = x ++ “ “ ++ concat2 xs concat2 [“make”, “into”, “bigstring”] => “make into bigstring”

Take and drop builds a sublist by –taking n elements from the front –dropping n elements from the front take 3 “abcde” => “abc” drop 3 “abcde” => “de” take,drop :: Int -> [a] -> [a] take n [] = [] take n (x:xs) | (n==0) = [] | otherwise = x: (take n-1 xs) drop n [] = [] drop n (x:xs) | (n==0) = (x:xs) | otherwise = drop (n-1) xs

Defining functions over lists Split the list in the middle –use (++) take drop –this method gives a higher level, more abstract view of lists - sometimes better for high level programming Split the first element from the rest –use (:) head tail –this method reflects the internal representation of lists, and is used to implement standard functions

Examples Define a function which sums the numbers in a list: sumlist :: [Int] -> Int sumlist [] = 0 sumlist (x:xs) = x+ sumlist xs Define a function which multiplies the numbers in a list: multlist :: [Int] -> Int multlist [] = 1 multlist x:xs = x * multlist xs

Define a function which returns the last element in a list: last :: [a] -> a first attempt last [] = [] last x:xs = last xs but, what about last [1]? last [x] =x last (x:xs) = last xs equations are tried in the order they are declared.