Comp 205: Comparative Programming Languages Higher-Order Functions Functions of functions Higher-order functions on lists Reuse Lecture notes, exercises,

Slides:



Advertisements
Similar presentations
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.
Advertisements

ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
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.
Cs776(Prasad)L7fold1 Fold Operations Abstracting Repetitions.
Prolog Programming (Volume 5) Dr W.F. Clocksin. List cells + Unification = Great Idea Normally we use ‘proper lists’, which are defined according to the.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Nested definitions Lists Lecture notes, exercises, etc., can.
Chapter 11 Proof by Induction. Induction and Recursion Two sides of the same coin.  Induction usually starts with small things, and then generalizes.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
12 Haskell.  ftp://web.ntnu.edu.tw/WWW/func_prog/ghc zip ftp://web.ntnu.edu.tw/WWW/func_prog/ghc zip  Haskell is  Lazy evaluated  Case-sensitive.
5/10/20151 GC16/3011 Functional Programming Lecture 13 Example Programs 1. Evaluating arithmetic expressions 2. lists as functions.
Factoring Trinomials 9-4 ax2 + bx +c
Comp 205: Comparative Programming Languages Functional Programming Languages: Haskell Lecture notes, exercises, etc., can be found at:
Cse536 Functional Programming 1 6/10/2015 Lecture #6, Oct 13, 2004 Reading Assignments –Read chapter 5 of the Text Polymorphic and Higher-Order Functions.
Comp 205: Comparative Programming Languages More User-Defined Types Tree types Catamorphisms Lecture notes, exercises, etc., can be found at:
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 20: Lists and Higher-Order Functions in Haskell COMP 144 Programming Language Concepts.
Comp 205: Comparative Programming Languages User-Defined Types Enumerated types Parameterised types Recursive types Lecture notes, exercises, etc., can.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Lecture notes, exercises, etc., can be found at:
Comp 205: Comparative Programming Languages Lazy Evaluation Errors Evaluation Strategies Infinite Lists…. Lecture notes, exercises, etc., can be found.
Chapter 5 Polymorphic and Higher-Order Functions.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 19: Functions, Types and Data Structures in Haskell COMP 144 Programming Language.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
Chapter 9: Functional Programming in a Typed Language.
11/1/20151 GC16/3011 Functional Programming Lecture 5 Miranda patterns, functions, recursion and lists.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
CS5205Haskell1 CS5205: Foundations in Programming Languages FP with Haskell A pure lazy functional language that embodies many innovative ideas in language.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
CMSC 330: Organization of Programming Languages Maps and Folds Anonymous Functions.
Recursion on Lists Lecture 5, Programmeringsteknik del A.
Scientific Notation Unit 1, Lesson 6. The Basics Scientific Notation means expressing numbers (usually very large numbers or very small numbers) in the.
CS5205Haskell1 CS5205: Foundation in Programming Languages Basics of Functional Programming.
Haskell Basics CSCE 314 Spring CSCE 314 – Programming Studio Using GHC and GHCi Log in to unix.cse.tamu.edu (or some other server) From a shell.
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.
1 5. Abstract Data Structures & Algorithms 5.1 Data Structure Fundamentals.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
Partial Fractions. After completing this chapter you should be able to: Add and subtract two or more fractions Convert an expression with linear factors.
Aims: To understand what a polynomial is To be able to add and subtract polynomials To be able to multiply polynomials Polynomials/Factorisation Lesson.
Spring 16 CSCI 4430, A Milanova/BG Ryder 1 Announcements HW5 due March 28 Homework Server link is up I will have office hours, Fri or Mon, check Announcements.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Functional Programming Language: Haskell (cont’d) Lecture 20 – Functional Programming, Spring 2008.
© 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.
Types CSCE 314 Spring 2016.
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
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CMSC 330: Organization of Programming Languages
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Higher Order Functions
PROGRAMMING IN HASKELL
Higher Order Functions
PROGRAMMING IN HASKELL
CS 457/557: Functional Languages Folds
Factorising quadratics
HIGHER ORDER FUNCTIONS
CSE 3302 Programming Languages
Announcements Quiz 5 HW6 due October 23
PROGRAMMING IN HASKELL
foldr and foldl applied to addition (using infix notation)
Higher-Order Functions in Haskell
GC16/3011 Functional Programming Lecture 9 Higher Order Functions
Presentation transcript:

Comp 205: Comparative Programming Languages Higher-Order Functions Functions of functions Higher-order functions on lists Reuse Lecture notes, exercises, etc., can be found at:

Write a Haskell function incAll that adds 1 to each element in a list of numbers. E.g., incAll [1, 3, 5, 9] = [2, 4, 6, 10] incAll :: [Int] -> [Int] incAll [] = [] incAll (n : ns) = n+1 : incAll ns

Write a Haskell function lengths that computes the lengths of each list in a list of lists. E.g., lengths [[1,3], [], [5, 9]] = [2, 0, 2] lengths ["but", "and, "if"]] = [3, 3, 2] lengths :: [[a]] -> [num] lengths [] = [] lengths (l : ls) = (length l) : lengths ls

Write a Haskell function map that, given a function and a list (of appropriate types), applies the function to each element of the list. map :: (a -> b) -> [a] -> [b] map f [] = [] map f (x : xs) = (f x) : map f xs

Using map incAll = map (plus 1) where plus m n = m + n lengths = map (length) Note that plus :: Int -> Int -> Int, so (plus 1) :: Int -> Int. Functions of this kind are sometimes referred to as partially evaluated.

Sections Haskell distinguishes operators and functions: operators have infix notation (e.g ), while functions use prefix notation (e.g. plus 1 2 ). Operators can be converted to functions by putting them in brackets: (+) m n = m + n. Sections are partially evaluated operators. E.g.: (+ m) n = m + n (0 :) l = 0 : l

Using map More incAll = map (1 +) addNewlines = map (++ "\n") halveAll = map (/2) squareAll = map (^2) stringify = map (: [])

Write a Haskell function product that multiplies all the elements in a list of numbers together. E.g., product [2,5,26,14] = 2*5*26*14 = 3640 product :: [Int] -> Int product [] = 1 product (n : ns) = n * product ns

Write a Haskell function flatten that takes a list of lists and appends all the lists together. flatten [[2,5], [], [26,14]] = [2,5,26,14] flatten ["mir", "and", "a"] = "miranda" flatten :: [[a]] -> [a] flatten [] = [] flatten (l : ls) = l ++ flatten ls

A polynomial (in x) is an expression of the form: ax 3 + bx 2 + cx + d The values a,b,c,d are called the coefficients. A polynomial can be represented as a list of the coefficients; e.g. [1, 3, 5, 2, 1] represents the polynomial: 1x 4 + 3x 3 + 5x 2 + 2x + 1 Write a Haskell function poly :: Int -> [Int] -> Int such that poly x [1,3,5,2,1] gives the polynomial above.

poly :: Int -> [Int] -> Int poly x ns = p where (p,l) = polyLen ns polyLen [] = (0,0) polyLen (a : as) = (a*(x^l1)+p1, l1+1) where (p1,l1) = polyLen as

Folding A general pattern for the functions product, flatten and polyLen is replacing constructors with operators. For example, product replaces : (cons) with * and [] with 1 : 1 : (2 : (3 : (4 : []))) 1 * (2 * (3 * (4 * 1))) We call such functions catamorphisms.

Folding Right Haskell has a built-in function, foldr, that does this replacement: foldr :: (a -> b -> b) -> b -> [a] -> b foldr f e [] = e foldr f e (x : xs) = f x (foldr f e xs)

Using foldr product = foldr (*) 1 flatten = foldr (++) [] poly x ns = p where (p,l) = polyLen ns polyLen = foldr addp (0,0) where addp a (b,c) = (a*(x^c)+b, c+1)

Using foldr More reverse :: [a] -> [a] reverse = foldr swap [] where swap h rl = rl ++ [h] reverse (1 : (2 : []))  swap 1 (swap 2 [])  (swap 2 []) ++ [1]  ([] ++ [2]) ++ [1]

More Efficient Reversing reverse = rev [] where rev l [] = l rev l (x:xs) = rev (x:l) xs

More Efficient Reversing reverse = rev [] where rev l [] = l rev l (x:xs) = rev (x:l) xs rev [] (1 : (2 : []))  rev (1:[]) (2 : [])

More Efficient Reversing reverse = rev [] where rev l [] = l rev l (x:xs) = rev (x:l) xs rev [] (1 : (2 : []))  rev (1:[]) (2 : [])  rev (2:(1:[])) []

More Efficient Reversing reverse = rev [] where rev l [] = l rev l (x:xs) = rev (x:l) xs rev [] (1 : (2 : []))  rev (1:[]) (2 : [])  rev (2:(1:[])) []  2 : (1 : [])

Folding Left Functions such as rev are called accumulators (because they accumulate the result in one of their arguments). They are sometimes more efficient, and can be written using Haskell's built-in function foldl : foldl :: (b -> a -> b) -> b -> [a] -> b foldl f e [] = e foldl f e (x : xs) = foldl f (f e x) xs

Using foldl rev = foldl swap [] where swap accList h = h : accList flatten = foldl (++) [] product = foldl (*) 1

Compose compose :: (b -> c) -> (a -> b) -> a -> c compose f g x = f (g x) There is a Haskell operator. that implements compose : (f. g) x = f (g x)

Using Compose poly x = fst. (foldr addp (0,0)) where addp a (p,l) = (a*(x^l)+p, l+1) makeLines :: [[char]] -> [char] makeLines = flatten. (map (++ "\n"))

Summary Functions are "first-class citizens" in Haskell: functions and operators can take functions as arguments. Programming points: catamorphisms and accumulators sections Next: user-defined types