Intro to Programming Functional Programming

Slides:



Advertisements
Similar presentations
Higher-Order Functions and Loops c. Kathi Fisler,
Advertisements

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Intro to Robots Robots are Everywhere. Intro to Robots Various robots in use today lawnmower pet baby seal for convalescents gutter cleaner home security.
2/28/2008. >>> Overview Arrays in Python – a.k.a. Lists Ranges are Lists Strings vs. Lists Tuples vs. Lists Map-Reduce Lambda Review: Printing to a file.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
CIT 590 Intro to Programming Lecture 4. Agenda Doubts from HW1 and HW2 Main function Break, quit, exit Function argument names, scope What is modularity!
8 For-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming 1.
CIT 590 Intro to Programming Object oriented programming.
CIT 590 Intro to Programming Lecture 5 – completing lists.
CS 100Lecture 131 Announcements Exam stats P3 due on Thursday.
Higher Order Functions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this.
CMSC 330: Organization of Programming Languages Maps and Folds Anonymous Functions.
CIT 590 Intro to Programming Lecture 10. Agenda Functional programming Functional programming as it is done in Python.
CIT 590 Intro to Programming Lecture 4. How are assignments evaluated Pay attention to what the HW says it is trying to teach you about ‘modular programming’
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Recursion Higher Order Functions CSCE 314 Spring 2016.
CIT 590 Intro to Programming Lecture 4. Random utilities to improve efficiency Search everything! Beyond Compare Keyboard shortcuts Not essentially but.
Python Basics 본 자료는 다음의 웹 사이트를 정리 한 내용이니 참조 바랍니다. ythonBasics.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Functional Processing of Collections (Advanced) 6.0.
MapReduce, Dictionaries, List Comprehensions Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
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.
List Algorithms Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Lecture #5 מבוא מורחב.
Higher Order Functions
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Introduction to Higher Order (Functional Programming) (Python) part 2
Programming in R Intro, data and programming structures
Spark.
CS 326 Programming Languages, Concepts and Implementation
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
COP4020 Programming Languages
Functional Processing of Collections (Advanced)
6.001 SICP Data abstractions
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Higher-Order Procedures
Programmazione ad alto livello con Python
List Algorithms Taken from notes by Dr. Neil Moore
LECTURE 31: INTRO TO FUNCTIONAL PROGRAMMING
Lecture #5 מבוא מורחב.
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
FP Foundations, Scheme In Text: Chapter 14.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
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.
Topic 1: Problem Solving
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Computer Science 111 Fundamentals of Programming I
Fundamentals of Functional Programming
(more) Python.
HIGHER ORDER FUNCTIONS
CSE 3302 Programming Languages
Lambda Functions, MapReduce and List Comprehensions
CISC101 Reminders All assignments are now posted.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSCE 314: Programming Languages Dr. Dylan Shell
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Assignment 3 due today.
List Comprehensions Problem: given a list of prices, generate a new list that has a 20% discount to each. Formally: input: list of old prices; output:
I can determine whether a relation is a function
More Scheme CS 331.
Generic Set Algorithms
Presentation transcript:

Intro to Programming Functional Programming CIT 590 Intro to Programming Functional Programming

What is a function? f(x) = 2x Very crudely put function turns inputs into outputs Can I pass a function as an argument to another function YES! The first time you see this, it looks crazy Just start thinking of a function as a machine that turns something into something else

Is functional programming actually used? YES YES YES C# uses it like you wouldn’t believe. Even more so if you use LINQ Scala, being taught in 591 uses it a lot! It offers way more flexibility Do not dismiss it the way I once did

Functional programming Functions taking other functions as an argument are called higher order functions We have already seen an example of this – sort Check out example of complex number sorting

Lambda functions Lambda x : x + 2 Lambda x, y : x + y think of lambda’s as ‘I am lazy and I do not want to write a function so let us do this instead’.

Map, filter and reduce Python provides this as Map – transform everything in the iterable (things you can loop through) Filter – filter those that return the value true Reduce – a two argument function ls1 = map(lambda x: x +2, a) ls2 = filter(lambda x: x%2 == 0, a) ls3 = reduce(lambda x, y : x+y, a )

Map filter reduce on strings names = ['Kevin Lee', 'Arvind Bhusnurmath', 'Di Lu', 'Ce Wang'] n1 = filter(lambda x: len(x) > 8, names) What is n1? [‘Kevin Lee’, ‘Arvind Bhusnurmath’] ‘Arvind Bhusnurmath’ [‘Arvind Bhusnurmath’] set([‘Kevin Lee’, ‘Arvind Bhusnurmath’]) (‘Kevin Lee’, ‘Arvind Bhusnurmath’)

Map filter reduce on strings names = ['Kevin Lee', 'Arvind Bhusnurmath', 'Di Lu', 'Ce Wang'] n2 = reduce(lambda x,y : x + y[0], names) What is n2? [‘KADC’] ‘KADC’ ‘Kevin Arvind Di Ce’ ‘Kevin LeeADC’ [‘Kevin’, ‘A’, ‘D’, ‘C’]

Map filter reduce on strings names = ['Kevin Lee', 'Arvind Bhusnurmath', 'Di Lu', 'Ce Wang'] n3 = map(lambda x : x.split()[-1] + x.split()[0], names) What is n3? [‘Lee Kevin’, ‘Bhusnurmath Arvind’, ‘Lu Di’, ‘Wang Ce’] [‘Lee’, ‘Bhusnurmath’, ‘Lu’, ‘Wang’] [‘Klee’, ‘Abhusnurmath’, ‘Dlu’, ‘Cwang’] None [‘LeeKevin’ ‘BhusnurmathArvind’, ‘LiDu’, ‘WangCe’]

List comprehensions [expr for var in list if expr] Basically a cool way of combining the map and filter functions [x*2 for x in a if x<2] These, thankfully, do not work by side effect Lst = [x*2 for x in a if x < 2] d = {'fred' : 1, 'arv': 5, 'li': 7, 'aash': 4, 'bye': 5} reduce(lambda x,y: (x+y)/2, [d[x]/2 for x in d.keys() if len(x)%2 == 0])

Sieve example Functional programming lends itself to recursion really easily Erasthothenes algorithm for getting rid of composite numbers in a list of increasing numbers beginning with the number 2 An ancient algorithm for finding prime numbers From any list of increasing numbers remove the multiples of any number in the list Easy for list of size 1 Can I do this recursively???

The 3 argument version of the reduce function We have already seem reduce(function, list) There is a 3 argument variant which is Reduce(function, list, identity element/first element for the process Sorting examples Define insertion of an element into a sorted list Now use a reduce operation

Quicksort List comprehension = badass  Quicksort.py

Common super useful reductions Reduce(lambda x,y : x+y ,a) Sigma/summation Reduce(lambda x,y: x*y , a, 1) product Reduce(lambda x,y: x + 1, a, 0) Num elements Reduce(lambda x,y: [y] + x, a, []) reverse the usage of reductions when dealing with lists