Functional Programming in Python Abhishek Dasgupta Indian Institute of Science Education and Research, Kolkata.

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

Introduction to OCaml Slides prepared by Matt Gruskin Some material borrowed from the CIS 500 lecture notes.
MATH 224 – Discrete Mathematics
Molecular Biomedical Informatics 分子生醫資訊實驗室 Web Programming 網際網路程式設計 1.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 21: Functional Programming in Python COMP 144 Programming Language Concepts Spring.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Functional Programming Notes for CSCE 190 Based on Sebesta,
CS 116 Tutorial 5 Introduction to Python. Review Basic Python Python is a series of statements def f(p1, p2,…pn): x = 5 if x > p1: x = p1 + p2 return.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
1 Programming Languages and Paradigms Functional Programming.
Functional Programming in Scheme and Lisp.
Higher Order Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
CIT 590 Intro to Programming Lecture 10. Agenda Functional programming Functional programming as it is done in Python.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Program State and Program Execution Version 2 – Discussing Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University.
Recursion Higher Order Functions CSCE 314 Spring 2016.
Python Data Structures By Greg Felber. Lists An ordered group of items Does not need to be the same type – Could put numbers, strings or donkeys in the.
Program State and Program Execution CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
MapReduce, Dictionaries, List Comprehensions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Machine Language Computer languages cannot be directly interpreted by the computer – they are not in binary. All commands need to be translated into binary.
Introduction to Concepts in Functional Programming CS16: Introduction to Data Structures & Algorithms Thursday, April 9,
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Functional Programming
Higher Order Functions
Intro to Programming Functional Programming
Introduction to Higher Order (Functional Programming) (Python) part 2
Theory of Computation Lecture 4: Programs and Computable Functions II
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
COP4020 Programming Languages
Application Development Theory
Flow of Control.
11/10/2018.
CHAPTER FOUR Functions.
Flow of Control.
Last Class We Covered Data representation Binary numbers ASCII values
PROGRAMMING IN HASKELL
Transforming Data (Python®)
FP Foundations, Scheme In Text: Chapter 14.
This Lecture Substitution model
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
Flow of Control.
CS 36 – Chapter 11 Functional programming Features Practice
Higher Order Functions
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
(more) Python.
Programming Languages
CSE 3302 Programming Languages
Lambda Functions, MapReduce and List Comprehensions
Loops and Arrays in JavaScript
This Lecture Substitution model
Introduction to Computer Science
PROGRAMMING IN HASKELL
Introduction to Programming
This Lecture Substitution model
Chapter 3: Selection Structures: Making Decisions
Laziness and Its Consequences
© Sangeeta M Chauhan, Gwalior
Types, Truth, and Expressions (Part 2)
Selamat Datang di “Programming Essentials in Python”
Lecture 2 - Names & Functions
Presentation transcript:

Functional Programming in Python Abhishek Dasgupta Indian Institute of Science Education and Research, Kolkata

What is Functional Programming? ● Most programs use procedural instructions ● a = a + 1, this simple statement is disallowed in pure functional programming (assignment can only be done to constants) ● Code modularised into functions ● FP: Everything is a function. ● Church's Thesis: Any intuitively computable function is recursive (can be expressed in terms of composition of functions)

Building blocks of FP in Python ● Are the following functions ● map ● reduce ● filter ● Along with lambda. (defines anonymous function, like >>> f = lambda x: x**2 >>> f(2) 4

operator module ● Contains functions corresponding to the logical operators and binary operators ● add ● mul ●... and so on... ● Useful in map() calls, no need to define trivial functions.

map ● map(function, list) Applies function to each element of a list and returns a new list ● Map is an alternative way of specify the familiar for loops: ● a=[] for i in range(10): a.append(i**2) map(lambda x: x**2, range(10)) alternative [x**2 for x in range(10)] (list-comprehension)

reduce ● Similar to foldr/foldl in traditional functional programming languages (like Haskell) ● reduce(function, list) Here function should be a binary function, then reduce applies function to first two elements of list, then takes the result and applies it to third element and so on: [1,2,3,4,5] would become ((((1.2).3).4).5) where. signifies the binary operation.

reduce ● You can sum a series in one line now! ● Earlier: s = 0 for i in range(1,10): s += 1.0/i ● Now: reduce(operator.add, map(lambda x: 1.0/x, range(1,10)))

filter ● filter(function, list) Applies the function to each item of a list; if the evaluation is True, then keeps the element in a new list. ● Example: filter even numbers >>> filter(lambda x: x % 2 == 0, range(10)) [0, 2, 4, 6, 8] >>> [x for x in range(10) if x % 2 == 0] [0, 2, 4, 6, 8]

Conclusion ● Functional programming not very hairy or complicated ● In fact, makes code simpler to understand ● Big monolithic functions replaced by atomic functions which do really one thing ● Makes us think about the code structure

An example: QuickSort def QuickSort(List): if List == []: return [] else: x = List[0] xs = List[1:] return QuickSort(filter(lambda l: l = x, xs)) ● Much simpler to code than the traditional way using a partition function.

Further Information ● Why Functional Programming matters ● Charming Python: Functional programming in Python ● Functional Programming HOWTO ● Functional Programming with Python (LG#109)