Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,

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.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Nested definitions Lists Lecture notes, exercises, etc., can.
Exercises – don’t use built in functions for these as we want the practice Write a recursive function to add up all the numbers in a list "flatten" a list.
0 LECTURE 5 LIST COMPREHENSIONS Graham Hutton University of Nottingham.
Comp 205: Comparative Programming Languages Functional Programming Languages: Haskell Lecture notes, exercises, etc., can be found at:
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.
A Lightning Tour of Haskell Lecture 1, Designing and Using Combinators John Hughes.
Comp 205: Comparative Programming Languages Lazy Evaluation and Infinite Lists Lecture notes, exercises, etc., can be found at:
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Lecture notes, exercises, etc., can be found at:
Advanced Programming Andrew Black and Tim Sheard Lecture 4 Intro to Haskell.
Comp 205: Comparative Programming Languages Imperative Programming Languages Functional Programming Languages Semantics Other Paradigms Lecture notes,
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 19: Functions, Types and Data Structures in Haskell COMP 144 Programming Language.
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.
Haskell Chapter 1, Part II. List Comprehension  List comprehensions are a way to filter, transform and combine lists  Similar to mathematical set comprehensions.
Functional Programming in Haskell Motivation through Concrete Examples Adapted from Lectures by Simon Thompson.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
Chapter 9: Functional Programming in a Typed Language.
Modeling with Haskell Scientific Seminar 03/04 Gerhard Navratil.
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.
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.
Functional Programming Lecture 3 - Lists Muffy Calder.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
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.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Functional Programming Language: Haskell (cont’d) Lecture 20 – Functional Programming, Spring 2008.
1 PROGRAMMING IN HASKELL An Introduction 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.
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}
Functional Programming
Theory of Computation Lecture 4: Programs and Computable Functions II
Functional Programming Lecture 12 - more higher order functions
A lightening tour in 45 minutes
Haskell Chapter 4.
Comp 205: Comparative Programming Languages
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Lecture 15 CS 1813 – Discrete Mathematics
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CS 457/557: Functional Languages Folds
CSE 3302 Programming Languages
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Presentation transcript:

Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises, etc., can be found at:

Recursion The type of lists is “recursively defined”: A list is either: empty, or is an element followed by a list Many function definitions follow this pattern: length [] = 0 unzip (x : xs) = 1 + length xs

Recursion The sum of all the numbers in the empty list is 0: sumAll (x : xs) = x + sumAll xs sumAll [] = 0 sumAll :: [Integer] -> Integer Compute the sum of all the numbers in a list: The sum of all the numbers in a list x : xs is x + the sum of all the numbers in xs :

Recursion Concatenating all the strings in the empty list gives the empty string: concat (s : ss) = s ++ concat ss concat [] = [] --- = “” concat :: [[Char]] -> [Char] Concatenate all the strings in a list of strings: Concatenating all the strings in a list s : ss is s appended to the concatenation of all the strings in ss :

Patterns Patterns can be combined: zip :: [Int] -> [String] -> [(Int,String)] zip [] ss = [] zip is [] = [] zip (i:is) (s:ss) = (i,s) : zip is ss

More Patterns Patterns can be complex: unzip [] = [] unzip ((x,y) : ps) = (x:xs, y:ys) where (xs,ys) = unzip ps

List Comprehensions Haskell (and other languages like Miranda) provides a special notation for constructing new lists from old: suppose xs = [1,2,3,4,5,6], then [ 2*x | x <- xs ] is the list [2,4,6,8,10,12].

Some Terminology [ E | P <- LExp ] body selector

List Comprehensions The selector can contain a pattern rather than simply a variable. For example, a function to add each pair in a List of pairs of numbers: addAll :: [(Int,Int)] -> [Int] addAll ps = [ x+y | (x,y) <- ps ]

List Comprehensions The selector can be combined with one or more guards, separated by commas: Main> [ 2*x | x<-[1..6], even x, x<5 ] [4,8] since the only elements of [1..6] that satisfy the guards are 2 and 4.

List Comprehensions More than one selector can be used: [ (x,y) | x <- [1,2,3], y <- [5,6] ] denotes the list [ (1,5), (1,6), (2,5), (2,6), (3,5), (3,6) ]

List Comprehensions List comprehensions are expressions, so they can be used in definitions: coords :: [(Int,Int)] coords = [ (x,y) | x<-[0..9], y<-[0..9]] mkCoords :: [Int] -> [Int] -> [(Int,Int)] mkCoords xs ys = [ (x,y) | x <- xs, y <- ys ]

Summary Key topics: Recursion List Comprehensions Next: Higher-Order Functions