Python functional programming. Functions are first-class objects Functions can be used as any other datatype, eg: Arguments to function Return values.

Slides:



Advertisements
Similar presentations
Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Advertisements

Python 2 Some material adapted from Upenn cis391 slides and other sources.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 21: Functional Programming in Python COMP 144 Programming Language Concepts Spring.
CSE (c) S. Tanimoto, 2007 Python Introduction 1 Introduction to Python for Artificial Intelligence Outline: Why Python? Interactive programming,
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions, Polymorphism.
1 Scheme Although Scheme is syntactically a simple language, it supports sophisticated modeling: higher-order functions are a powerful tool in describing.
C. Varela1 Programming Languages (CSCI 4430/6969) Lambda Calculus Carlos Varela Rennselaer Polytechnic Institute September 17, 2007.
C. Varela1 Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators Carlos Varela Rennselaer.
Introduction to Python II CIS 391: Artificial Intelligence Fall, 2008.
Functions in Python. The indentation matters… First line with less indentation is considered to be outside of the function definition. Defining Functions.
Introduction to Python II CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth January 2005.
Python Control of Flow.
List Comprehensions. Python’s higher-order functions  Python supports higher-order functions that operate on lists similar to Scheme’s >>> def square(x):
Python, Part 2. Python Object Equality x == y x is y In Java: (x.equals(y)) (x == y)
Introduction to Functional Programming (SCALA) John Woodward.
12 April 2009Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Formal.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming 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.
Functional Programming in Scheme and Lisp.
Iterators, Linked Lists, MapReduce, Dictionaries, and List Comprehensions... OH MY! Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 6 Defining Functions.
Python Functions.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
An Introduction. What is Python? Interpreted language Created by Guido Van Rossum – early 90s Named after Monty Python
Python – Part 3 Functions 1. Getting help Start the Python interpreter and type help() to start the online help utility. Or you can type help(print) to.
Generalizing Over Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Computer Science 111 Fundamentals of Programming I Default and Optional Parameters Higher-Order Functions.
CIT 590 Intro to Programming Lecture 10. Agenda Functional programming Functional programming as it is done in Python.
Chapter 10 Loops: while and for CSC1310 Fall 2009.
LECTURE 3 Python Basics Part 2. FUNCTIONAL PROGRAMMING TOOLS Last time, we covered function concepts in depth. We also mentioned that Python allows for.
The Python Language Petr Přikryl Part IIb Socrates IP, 15th June 2004 TU of Brno, FIT, Czech Republic.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
Cs3180 (Prasad)L156HOF1 Higher-Order Functions. cs3180 (Prasad)L156HOF2 Equivalent Notations (define (f x y) (… body …)) = (define f (lambda (x y) (…
Domain: a set of first elements in a relation (all of the x values). These are also called the independent variable. Range: The second elements in a relation.
12.01 Multiplying Monomials. A monomial is a number, a variable, or a product of both. Examples: 8, x, 5y, x 3, 4x 2, – 6xy 7 Exponential Notation amam.
Second Order Programming Intro2CS – week 10B 1. Functions are values too 2.
CSE 341 : Programming Languages Lecture 9 Lexical Scope, Closures Zach Tatlock Spring 2014.
Built-In Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
MapReduce, Dictionaries, List Comprehensions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Introduction to Concepts in Functional Programming CS16: Introduction to Data Structures & Algorithms Thursday, April 9,
Functional Programming in Python Abhishek Dasgupta Indian Institute of Science Education and Research, Kolkata.
Multiplying with exponents
Introduction to Higher Order (Functional Programming) (Python) part 2
The Environment Model*
CSC1018F: Functional Programming
Coconut Sandor aguilar.
Functional.
Python 2 Some material adapted from Upenn cis391 slides and other sources.
CSC1018F: Intermediate Python
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
Python Functions Part I Presented by : Sharmin Abdullah
Simplifying Expressions
(more) Python.
Lambda Functions, MapReduce and List Comprehensions
CSC1018F: Intermediate Python (Tutorial)
Completing the Square.
CSC1018F: Functional Programming (Tutorial)
Functions Functions being objects means functions can be passed into other functions: sort (list, key=str.upper)
Functional Programming
GC16/3011 Functional Programming Lecture 9 Higher Order Functions
Python Basics. Topics Features How does Python work Basic Features I/O in Python Operators Control Statements Function/Scope of variables OOP Concepts.
Functions John R. Woodward.
© Sangeeta M Chauhan, Gwalior
Presentation transcript:

Python functional programming

Functions are first-class objects Functions can be used as any other datatype, eg: Arguments to function Return values of functions Assigned to variables Parts of tuples, lists, etc >>> def square(x): return x*x >>> def applier(q, x): return q(x) >>> applier(square, 7) 49

Lambda Notation Python’s lambda creates anonymous functions >>> lambda x: x + 1 at 0x1004e6ed8> >>> f = lambda x: x + 1 >>> f at 0x1004e6f50> >>> f(100) 101

Lambda Notation Be careful with the syntax >>> f = lambda x,y: 2 * x + y >>> f at 0x87d30> >>> f(3, 4) 10 >>> v = lambda x: x*x(100) >>> v at 0x87df0> >>> v = (lambda x: x*x)(100) >>> v 10000

Lambda Notation Limitations  Note: only one expression in the lambda body; Its value is always returned  The lambda expression must fit on one line!  Lambda will probably be deprecated in future versions of python Guido is not a lambda fanboy

Functional programming  Python supports functional programming idioms  Builtins for map, reduce, filter, closures, continuations, etc.  These are often used with lambda

Example: composition >>> def square(x): return x*x >>> def twice(f): return lambda x: f(f(x)) >>> twice >>> quad = twice(square) >>> quad at 0x87d30> >>> quad(5) 625

Example: closure >>> def counter(start=0, step=1): x = [start] def _inc(): x[0] += step return x[0] return _inc >>> c1 = counter() >>> c2 = counter(100, -10) >>> c1() 1 >>> c2() 90

map >>> def add1(x): return x+1 >>> map(add1, [1,2,3,4]) [2, 3, 4, 5] >>> map(lambda x: x+1, [1,2,3,4]) [2, 3, 4, 5] >>> map(+, [1,2,3,4], [100,200,300,400]) map(+,[1,2,3,4],[100,200,300,400]) ^ SyntaxError: invalid syntax

map  + is an operator, not a function  We can define a corresponding add function >>> def add(x, y): return x+y >>> map(add,[1,2,3,4],[100,200,300,400]) [101, 202, 303, 404]  Or import the operator moduleoperator >>> from operator import * >>> map(add, [1,2,3,4], [100,200,300,400]) [101, 202, 303, 404] >>> map(sub, [1,2,3,4], [100,200,300,400]) [-99, -198, -297, -396]

filter, reduce  Python has buiting for reduce and filter >>> reduce(add, [1,2,3,4]) 10 >>> filter(odd, [1,2,3,4]) [1, 3]  The map, filter and reduce functions are also at risk 