Com2010 - Functional Programming Higher Order Functions and Computation Patterns (I) Marian Gheorghe Lecture 10 Module homepage Mole &

Slides:



Advertisements
Similar presentations
Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004.
Advertisements

Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Higher-Order Functions Koen Lindström Claessen. What is a “Higher Order” Function? A function which takes another function as a parameter. Examples map.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Computer Science 209 The Strategy Pattern II: Emulating Higher-Order Functions.
1 Perl Perl basics Perl Elements Arrays and Hashes Control statements Operators OOP in Perl.
Slide 1 Vitaly Shmatikov CS 345 Introduction to Scheme.
Functional Languages. Why? Referential Transparency Functions as first class objects Higher level of abstraction Potential for parallel execution.
Com Functional Programming Higher Order Functions and Computation Patterns (II) Marian Gheorghe Lecture 11 Module homepage Mole &
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
©University of Sheffieldcom2010 Com Functional Programming Syntax Analysis Marian Gheorghe Lecture 16 Module homepage Mole &
Chapter Fifteen: Functional Programming Languages Lesson 12.
Going Functional Primož Gabrijelčič. Functional programming.
Com Functional Programming Regular Expressions and Abstract Data Types Marian Gheorghe Lecture 14 Module homepage Mole &
Overview of the Haskell 98 Programming Language
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.
TIVDM2Functional Programming Language Concepts 1 Concepts from Functional Programming Languages Peter Gorm Larsen.
Com Functional Programming Lazy Evaluation Marian Gheorghe Lecture 13 Module homepage Mole & ©University of Sheffieldcom2010.
1-1 An Introduction to Functional Programming Sept
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 1 Overview A good programming language is.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
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,
Cs3180 (Prasad)L156HOF1 Higher-Order Functions. cs3180 (Prasad)L156HOF2 Equivalent Notations (define (f x y) (… body …)) = (define f (lambda (x y) (…
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
Lecture 16: Advanced Topic: Functional Programming CS5363 Compiler and Programming Languages.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
©University of Sheffieldcom2010 Com Functional Programming Demos: LexPrs & PrsRes & Software Engineering Design and Coding Marian Gheorghe Lecture.
Functional Programming
Lecture 14: Advanced Topic: Functional Programming
CS314 – Section 5 Recitation 9
Programming Languages 2nd edition Tucker and Noonan
Functional Programming
CS314 – Section 5 Recitation 10
Conditional Expressions
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
dr Robert Kowalczyk WMiI UŁ
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Introduction to Higher Order (Functional Programming) (Python) part 2
Theory of Computation Lecture 4: Programs and Computable Functions II
Information Science and Engineering
Lecture 2:Data Types, Functional Patterns, Recursion, Polymorphism
Principles of programming languages 4: Parameter passing, Scope rules
A lightening tour in 45 minutes
Haskell.
PROGRAMMING IN HASKELL
Higher-Order Functions
PROGRAMMING IN HASKELL
Learning to Program in Python
PROGRAMMING IN HASKELL
FP Foundations, Scheme In Text: Chapter 14.
PROGRAMMING IN HASKELL
Programming Languages 2nd edition Tucker and Noonan
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Material in the textbook Sections to 1.2.1
6.001 SICP Variations on a Scheme
PROGRAMMING IN HASKELL
Functional Programming and Haskell
Chapter 15 Functional Programming 6/1/2019.
PROGRAMMING IN HASKELL
GC16/3011 Functional Programming Lecture 9 Higher Order Functions
Functional Programming and Haskell
Presentation transcript:

Com Functional Programming Higher Order Functions and Computation Patterns (I) Marian Gheorghe Lecture 10 Module homepage Mole & ©University of Sheffieldcom2010

Teaching; weeks 7-9, 3h/week Project; weeks Topics: 1.Higher order functions, Algebraic data types, Lazy programming, Regular expressions, Abstract data types: 5 h – concepts 2.Lexical analysis and parsing: 3h – case study, Soft Eng approach 3.Demo Lex & Parse: 1h 1&2 links with other topics –machine, languages, distributed computation Project discussion: 1h (TBA) Timetable: Mon: pm; Wed: am; Thu: pm Haskell II -Summary ©University of Sheffieldcom2010

Different (functional vs imperative) Natural constructs (implement TCS concepts: set, function, morphism, recursion) Easy to understand and use (?!) Useful… Why Functional Programming? ©University of Sheffieldcom2010

Python is an object-oriented language used in software development It has the following characteristics Very clear, readable syntax Intuitive object orientation Natural expression of procedural code Full modularity and packages Exception-based error handling Similarities to C, C++, Java etc. Example ©University of Sheffieldcom2010

we can write an initial sub-sequence of the Fibonacci series as follows: # Fibonacci series:... # the sum of two elements defines the next... a, b = 0, 1 while b < 10 print b a, b = b, a+b Example – OO style ©University of Sheffieldcom2010

but… we can define functions >>> def cube(x): return x*x*x and map them >>> map(cube, range(1, 11)) [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000] it supports list comprehension >>> vec = [2, 4, 6] >>> [3*x for x in vec] [6, 12, 18] and membership testing set(['orange', 'pear', 'apple', 'banana']) >>> 'orange' in fruit # fast membership testing True Example – Functional style ©University of Sheffieldcom2010

18. Higher-Order Functions and Computation Patterns 18.1 The function type a->b 18.2 Arity and infix 18.3 Iteration and primitive recursion 18.4 Efficiency and recursion patterns 18.5 Partial functions and errors 18.6 More higher-order on lists Summary ©University of Sheffieldcom2010

In any functional programming language that deserves its name, functions are first-class citizens. They form a data type and thus can be passed as arguments and returned as values. Functions of type (a->b) -> c take functions of type a->b as input and produce a result of type c. Functions of type a -> (b->c) take values of type a as arguments and produce functions of type b -> c as results. HOF - Introduction ©University of Sheffieldcom2010

Definition. A function is higher-order if it takes a function as an argument or returns a function as a result, or both. Higher-order functions we have seen in Chapter 16.2 map :: (a->b)->[a]->[b] filter :: (a->Bool)->[a]->[a] foldr :: (a->b->b)->b->[a]->b merge :: (a->a->Bool)->[a]->[a]->[a] mergesort :: (a->a->Bool)->[a]->[a] Do you remember what they do? HOF - Definition ©University of Sheffieldcom2010

18.1 The function type a->b (“Haskell is strongly typed” – PDG’s notes, p.7) Objects of type a->b are constructed by lambda abstraction \x->e and used in function application f e’. Lambda abstraction: if e has type b and x is a variable of type a then \x->e has type a->b Function application: if f has type a->b and e’ has type a then f e’ has type b Expressions such as \x->e are also called lambda expressions, or anonymous functions, in contrast to functions that are declared and bound to a name by definition equations. Function type ©University of Sheffieldcom2010

The function definition double::Int->Int double x=2*x defines the behaviour of double point-wise. i.e. for every argument x. This definition has the same effect as double::Int->Int double=(\x->2*x) defines double wholesale. The anonymous function \x->2*x is an expression for the complete function. -> associates to the right. We may write a->b->c->d instead of a->(b->(c->d)). Similarly, \x -> \y -> \z -> x + y + z rather than \x ->(\y ->(\z->x + y + z)); \x y z->x + y + z Function type: Example ©University of Sheffieldcom2010

A simple example of an operator on functions is function composition: fcomp::(b->c)->(a->b)->a->c fcomp g f x=g (f x) Function composition - its own operator symbol in Haskell Prelude: (.)::(b->c)->(a->b)->a->c (g.f) x=g (f x) or (g.f)=(\x->g (f x)) This is called an infix operator definition Function composition ©University of Sheffieldcom2010

Type definitions in Haskell. A type definition for a function is foo :: a1->a2 …->an->t where ai and t are type expressions. The types in the list a1 a2 … an refer to the parameters. Their number n is the so-called arity of foo. A typical function definition for foo consists of guarded equations foo p1 p2 … pn foo p1 | g1 = b1 … | gk = bk Types and arity ©University of Sheffieldcom2010

For functions of arity 2 we can use infix notation. For instance, (.)::(b->c)->(a->b)->a->c (.)(g f) x=g (f x) which is usually written as (g.f) in infix notation. Ex: eightTimes::(Int -> Int) eightTimes=double. double. double as abbreviation for (.) double ((.) double double)) or double. (double. double) Infix operators ©University of Sheffieldcom2010

The function ++ concatenates two strings: (++)"Name=" "Bill" ⇒ "Name=Bill" Its type is String -> String -> String. According to the rules of function application we can also apply it to only one argument: infixr 5 ++ (++)::String->(String->String) (++)”Name=”::String->String The result (++) "Name = " is a one-argument prefAll::[String]->[String] -- prefix all elements in a list of strings prefAll names = map((++)"Name=")names prefAll ["Bill","John","Tony"] ⇒ ["Name=Bill","Name=John","Name=Tony"] Partial applications- application of prefix definition ©University of Sheffieldcom2010

Example. The function exp2::Int->Int could be mathematically defined (using double ) as follows: exp2(n)=2 n =2*(2 n-1 )=2*exp2(n-1)=double(exp2(n-1)), n>0 exp2(0)=1 We may define exponentiation with base 2 by a recursive computation exp2::Int->Int exp2 n | n==0 = 1 | n>0 = double(exp2 (n-1)) (the last line may be written differently) Obs. For every (positive) n the expression exp2 n iterates the function double Iteration - example ©University of Sheffieldcom2010

The process of iterating a function is very general: myIter::(a->a)->a->Int->a iterates a function of type a->a starting with an initial value (of type a), for a given number of steps (type Int ) myIter::(a->a)->a->Int->a -- 1st param: iteration function -- 2nd param: initial value -- 3rd param: iteration length myIter f x n | n==0 = x | n>0 = f (myIter f x (n-1)) The polymorphic higher-order function myIter captures the abstract process of iteration. Iteration - pattern ©University of Sheffieldcom2010

Example -Exponentiation: myExp2::(Int->Int) myExp2 = myIter double 1 Consider the problem of computing the exponent b n for arbitrary b. exp b n =b n, n>0; exp b 0 =1 All we do is replace double by the anonymous function \x->b*x: myExp::Int->(Int->Int) myExp b = myIter (\x->b*x) 1 Example. Construct a list [‘c’,...,’c’] repChar::Char->(Int->[Char]) repChar c = myIter (\x->c:x) [] Obs. Note the conciseness of myIter and the use of anonymous function Iteration. Example revisited ©University of Sheffieldcom2010