Introduction to Concepts in Functional Programming CS16: Introduction to Data Structures & Algorithms Thursday, April 9, 2015 1.

Slides:



Advertisements
Similar presentations
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Advertisements

Python Programming Chapter 5: Fruitful Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CSE341: Programming Languages Lecture 12 Equivalence Dan Grossman Spring 2013.
Lists Introduction to Computing Science and Programming I.
CSC1016 Coursework Clarification Derek Mortimer March 2010.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Introduction to JavaScript for Python Programmers
CS TShirts! Congratulations to Emily Kasbohm, whose T-Shirt design won! We will set up a Teespring campaign, and a link to the class if you.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
CS61A Lecture 2 Functions and Applicative Model of Computation Tom Magrino and Jon Kotker UC Berkeley EECS June 19, 2012.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Lists in Python.
Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca.
Invitation to Computer Science, Java Version, Second Edition.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
1 Programming Languages and Paradigms Functional Programming.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Built-in Data Structures in Python An Introduction.
Functional Programming in Scheme and Lisp.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Python Functions.
I Power Higher Computing Software Development High Level Language Constructs.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
CIT 590 Intro to Programming Lecture 10. Agenda Functional programming Functional programming as it is done in Python.
Functional Programming IN NON-FUNCTIONAL LANGUAGES.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
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.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
Functional Programming in Python Abhishek Dasgupta Indian Institute of Science Education and Research, Kolkata.
Functional Programming
CS314 – Section 5 Recitation 9
Functional Programming
Introduction to Higher Order (Functional Programming) (Python) part 2
CS 326 Programming Languages, Concepts and Implementation
ECS10 10/10
EGR 2261 Unit 4 Control Structures I: Selection
Topic: Functions – Part 2
CS 115 Lecture 8 Structured Programming; for loops
Functional Processing of Collections (Advanced)
Chapter 4 – Control Structures Part 1
Introduction to Functional Programming in Racket
PROGRAMMING IN HASKELL
Important Concepts from Clojure
Important Concepts from Clojure
COS 260 DAY 10 Tony Gauvin.
FP Foundations, Scheme In Text: Chapter 14.
Distributed System Gang Wu Spring,2018.
PROGRAMMING IN HASKELL
Loops CIS 40 – Introduction to Programming in Python
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Fundamentals of Functional Programming
Introduction to Functional Programming in Racket
(more) Python.
CISC101 Reminders All assignments are now posted.
Important Concepts from Clojure
CSE341: Programming Languages Lecture 12 Equivalence
Lecture 2 - Names & Functions
Presentation transcript:

Introduction to Concepts in Functional Programming CS16: Introduction to Data Structures & Algorithms Thursday, April 9,

Outline State Functions as Building Blocks Higher order Functions Map Reduce Thursday, April 9,

What is Functional Programming? Functional programming is a style of programming that is fairly different from what you’ve done in CS15/CS16 Examples of functional languages: Haskell, Clojure, Common Lisp, OCaml Important features that we’ll discuss: Programs are built entirely of functions Computations do not change program state Thursday, April 9,

Functions In math: f(x) = x + 1 In python: def f(x): return x + 1 Every input maps to exactly one output A value is always returned Thursday, April 9,

What is State? State is defined by all stored information to which a program has access at any given time Mutable data is data that can be changed after creation. Mutating data changes program state. Immutable data cannot be changed after creation. A language with only immutable data can never change program’s state. Thursday, April 9,

State Continued Some paradigms (like the ones you’ve seen in CS15/16) operate by manipulating program state Some familiar examples: Mutator methods Methods/functions that don’t return anything Loops that mutate local variables (such as the incrementation variable) Thursday, April 9,

State Continued This also means that the same function can behave on the data in an entirely different way depending on the program state Example: Simulation of driving behavior based on the the color of a stoplight Thursday, April 9,

Stateful Program Example light_color = “RED” def change_light(): //Mutates light_color based on it’s current value if light_color is “RED”: light_color = “GREEN” if light_color is “YELLOW”: light_color = “RED” if light_color is “GREEN”: light_color = “YELLOW” //Note that this does not have to return anything to impact the program! def drive(car): if light_color is “RED”: car.stop() if light_color is “YELLOW”: car.slow_down() if light_color is “GREEN”: car.move_forward() Thursday, April 9, //This function behaves differently based on the value of light_color

Functional Programming and State Pure functional languages avoid changing state, and all data is immutable What does this imply? Functions are deterministic: the same input will always give the same output Programs do not contain side effects (see upcoming example) Order of execution is no longer important Functions are easier to reuse elsewhere Thursday, April 9,

Mutable vs Immutable State Example Imagine you have these 2 programs: Will they produce the same result if they are allowed to change state? What if all state is immutable? Thursday, April 9, Program 1Program 2 a = f(x) b = g(y) return h(a, b) b = g(y) a = f(x) return h(a, b)

Mutable vs Immutable State Example Continued Imagine you have these 2 programs: Mutable State: These are not guaranteed to be the same because the first function call might change state, resulting in different behavior of the second function call. This is an example of a side effect. Immutable State: These will ALWAYS produce the same result Thursday, April 9, Program 1Program 2 a = f(x) b = g(y) return h(a, b) b = g(y) a = f(x) return h(a, b)

State and Loops Another place you have commonly seen mutable state is in for loops and while loops. Without mutable state, traditional for loops and while loops cannot exist! Many functional languages do not have loop constructs. Instead, they make use of recursion. Thursday, April 9,

Why No Loops? Loops often depend on mutable variables. def max(input_list): max = -infinity for i from 0 to len(input_list): if input_list[i] > max: max = input_list[i] return max Where is state being mutated? Thursday, April 9,

Why No Loops Continued def max(input_list): max = -infinity for i from 0 to len(input_list): //changing ‘i’ if input_list[i] > max: max = input_list[i] //changing ‘max’ return max This will not work if all variables are immutable In a functional language, you would have to write this without mutation. How…? Thursday, April 9,

Replacing Iteration with Recursion The version on the right would work in a pure functional language. The version on the left would not. 15 Iterative Max Function (With Mutation)Recursive Max Function (Without Mutation) def max(input_list): max = -infinity for i from 0 to len(input_list): if input_list[i] > max: max = input_list[i] return max def max(input_list): return max_helper(input_list, -infinity) def max_helper(input_list, max): if len(input_list) == 0: return max if input_list[0]> max: return max_helper(input_list[1:], input_list[0]) return max_helper(input_list[1:], max) Thursday, April 9, 2015

First Class Functions In the functional paradigm, functions are first class i.e. they can be passed around and returned like any other data As an example: def add_one(x): return x + 1 def apply_function_to_five(f): return f(5) print apply_function_to_five(add_one) >> 6 Thursday, April 9,

First Class Functions Let’s look more closely at our add_one function: def add_one(x): return x + 1 What is actually happening here? We’re simply binding a function to the identifier add_one In python (which supports aspects of functional programming), this is 100% equivalent to: add_one = lambda x: x+1 Thursday, April 9, More on this syntax soon!

First Class Functions Now our function looks much more similar to the way we usually express bound data Examples: a = 23 //binding a number b = “David” //binding a string c = True //binding a boolean d = lambda x: x+1 //binding a function Thursday, April 9,

Anonymous Functions Numbers, booleans, strings etc. do not have to be bound in a program. Neither do functions! Anonymous Function: a function that is not bound to an identifier Python Examples: lambda x: x+1 // Input: a number k // Output: a function that increments k by a number def increment_by_k_function(k): return lambda x: return x + k Thursday, April 9,

Function Syntax Overview Thursday, April 9, MathBound Python Function Anonymous Function in Python f(x) = x + 1 def f(x): return x + 1 or f = lambda x: x+1 lambda x: x + 1

Higher Order Functions A higher order function is one that takes in one or more functions as input or outputs a function. You’ve already seen functions that take other functions as input! Example from earlier in these slides: print apply_function_to_five(add_one) >> 6 Thursday, April 9,

Higher Order Functions But what does it look like to output a function? >> def add_me_func(x): return lambda y: x + y //returns an unsatisfied function // 1 is the value passed in for ‘x’ in a >> add_one = add_me_func(1) >> print add_one // this prints the value of add_one, which is a function object returned by calling add_me_func(1) at 0x123e410> // 5 corresponds to the parameter ‘y’ in the function returned by add_me_func(1) which is lambda y: 1 + y >> print add_one(5) 6 Thursday, April 9,

Higher Order Functions You can even do both in one function! >> def satisfy_first_arg(func, x): return lambda y: func(x,y) //satisfies one arg >> def add(x,y): return x + y // ‘add’ corresponds to the argument ‘func’ and 1 corresponds to the argument ‘x’ in satisfy_first_arg(func,x) >> add_one = satisfy_first_arg(add, 1) // add_one represents the function lambda y: add(1, y) at 0x123e410> // this evaluates and prints lambda 7: add(1,7) >> print add_one(7) 8 Thursday, April 9,

Map Map is a higher order function that applies a function to all elements in a list, and returns a new list. Inputs: f - a function x - a collection of elements Output: a collection of elements, with f applied to each of the elements of x Thursday, April 9,

Map Thursday, April 9, f(x) = x In python: map(lambda x: x-2, [11,24,16,-5,34,4,66])

Reduce (also called Fold) Reduce is a bit trickier to wrap your head around General idea: It reduces a whole list to a return value using some binary function to combine the elements Inputs: f - binary function x - list of elements acc - accumulator (what will be updated and returned) Outputs y - the value of f sequentially applied and tracked in acc Thursday, April 9,

Reduce Reduce is roughly equivalent to this python function: def reduce (binary_function, data_structure, accumulator): for x in data_structure: accumulator = binary_function(accumulator, x) return accumulator Thursday, April 9,

Reduce Example f = lambda x,y: x + y reduce(f, [1,2], 0) Thursday, April 9, Binary function Input Collection Accumulator

Reduce f = lambda x,y: x + y reduce(f, [1,2], 0) Thursday, April 9, ((0+1)+2) = reduce(f,[1,2],0)= Current Accumulator

Reduce f = lambda x,y: x + y reduce(f, [1,2], 0) Thursday, April 9, ((0+1)+2) = (1+2) = reduce(f,[1,2],0)= reduce(f,[2],1)= Current Accumulator

Reduce f = lambda x,y: x + y reduce(f, [1,2], 0) Thursday, April 9, ((0+1)+2) = (1+2) = 3 reduce(f,[1,2],0)= reduce(f,[2],1)= 3 Final Accumulator/Return Value

32 Reduce f = lambda x,y: x*y reduce(f, [1,2,3,4,5], 1)

Reduce f = lambda x,y: x*y reduce(f, [1,2,3,4,5], 1) Thursday, April 9, (((((1*1)*2)*3)*4)*5) = ((((1*2)*3)*4)*5) = (((2*3)*4)*5) = reduce(f,[1,2,3,4,5],1) = reduce(f,[2,3,4,5],1)= reduce(f,[3,4,5],2)= Current Accumulator

Reduce f = lambda x,y: x*y reduce(f, [1,2,3,4,5], 1) Thursday, April 9, (((((1*1)*2)*3)*4)*5) = ((((1*2)*3)*4)*5) = (((2*3)*4)*5) = ((6*4)*5) = reduce(f,[1,2,3,4,5],1) = reduce(f,[2,3,4,5],1)= reduce(f,[3,4,5],2)= reduce(f,[4,5],6)= Current Accumulator

Reduce f = lambda x,y: x*y reduce(f, [1,2,3,4,5], 1) Thursday, April 9, (((((1*1)*2)*3)*4)*5) = ((((1*2)*3)*4)*5) = (((2*3)*4)*5) = ((6*4)*5) = (24*5) = reduce(f,[1,2,3,4,5],1) = reduce(f,[2,3,4,5],1)= reduce(f,[3,4,5],2)= reduce(f,[4,5],6)= reduce(f,[5],24)= Current Accumulator

Reduce f = lambda x,y: x*y reduce(f, [1,2,3,4,5], 1) Thursday, April 9, (((((1*1)*2)*3)*4)*5) = ((((1*2)*3)*4)*5) = (((2*3)*4)*5) = ((6*4)*5) = (24*5) = 120 reduce(f,[1,2,3,4,5],1) = reduce(f,[2,3,4,5],1)= reduce(f,[3,4,5],2)= reduce(f,[4,5],6)= reduce(f,[5],24)= 120 Final Accumulator/Return Value

Reduce The accumulator doesn’t always have to be an integer, and reduce doesn’t always have to reduce a list into a single number Example: removing consecutive duplicates, the accumulator will be a list! def compress(my_list): f = lambda acc, e: acc + [e] if acc[-1] is not e else acc return reduce(f, my_list, [my_list[0]]) Thursday, April 9,

Reduce The accumulator doesn’t always have to be an integer, and reduce doesn’t always have to reduce a list into a single number Example: removing consecutive duplicates, the accumulator will be a list! def compress(my_list): f = lambda acc, e: acc + [e] if acc[-1] is not e else acc return reduce(f, my_list, [my_list[0]]) Thursday, April 9, Note: This way of writing if/else is called a ternary operator

Higher Order Functions With higher order functions, we can build abstractions for many constructs and make programs much more concise. Let’s revisit the recursive max function we saw earlier: Thursday, April 9, Original Recursive ExampleRevised with Higher Order Functions def max(input_list): return max_helper(input_list, -infinity) def max_helper(input_list, max): if len(input_list) == 0: return max if input_list[0]> max: return max_helper(input_list[1:], input_list[0]) return max_helper(input_list[1:], max) def max(input_list): find_max = lambda acc, elem: elem if elem > acc else acc return reduce(find_max, input_list, -infinity)

Building Programs With all of this information, we can start to see how programs can be built in a functional style Programs can be thought of as one giant function composition f(g(...h(x)...)). Pretty Cool! In pure functional languages, programs can not be expressed as a series of instructions because this type of programming requires mutation. Thursday, April 9,

Advantages and Disadvantages of Functional Programming Thursday, April 9, AdvantagesDisadvantages

Advantages and Disadvantages of Functional Programming It’s difficult to pick out advantages vs disadvantages because FP is simply better suited for certain problems, but here are a few: Thursday, April 9, AdvantagesDisadvantages ●Programs are deterministic ●Code is often elegant and concise because of the expressive abstractions ●Code is easy to run concurrently because everything is immutable ●Learning functional programming teaches you to think about problems in a different way, which ultimately makes you a better problem solver ●Programs are deterministic ●Potential performance losses because of the amount of garbage collection – takes up more memory, and takes longer to create new variables when you want to fake “mutate” them ●I/O is difficult because it requires interaction with state ●It’s very different from imperative programming, so many programmers find it difficult to learn

Functional Programming Practice Thursday, April 9, Syntax Examples: lambda x: x + 1 map(lambda x: x-2, [11,24,16,-5,34,4,66]) reduce(f, [1,2], 0)

Functional Programming Practice Thursday, April 9, Problem: Write an anonymous function that raises a single argument n to the nth power

Functional Programming Practice Thursday, April 9, Solution: lambda n: n**n

Functional Programming Practice Thursday, April 9, Problem: Write a line of code that applies the function you wrote in part 1 to an input list

Functional Programming Practice Thursday, April 9, Solution: map(lambda n: n**n, list)

Functional Programming Practice Thursday, April 9, Problem: Write an anonymous function that takes in a single argument n and returns a function that consumes no arguments and returns n

Functional Programming Practice Thursday, April 9, Solution: lambda n: lambda: n

Functional Programming Practice Thursday, April 9, Problem: Write a line of code that applies the function you wrote in part 3 to an input list. This should give you a list of functions. Next, write a line of code that takes in that list of functions and turns it back into the original list.

Functional Programming Practice Thursday, April 9, Solution: Part 1: function_list = map(lambda n: lambda: n, list) Part 2: map(lambda n: n(), function_list)

Functional Programming Practice Thursday, April 9, Problem: Remove odd numbers from a list using reduce

Functional Programming Practice Thursday, April 9, Solution: def remove_odds(my_list): return reduce(lambda x, y: x + [y] if y%2 is 0 else x, my_list, [])

Higher Order Functions There are a number of commonly-used higher- order functions We only got to talk about map and reduce. But there are many more! Thursday, April 9,

Higher Order Functions f- function, may have to be of certain arity x - collection of elements y - collection of elements filter(f,x) returns a list of only those in x that make f true – f is a function that evaluates to true or false based on the input zipWith(f,x,y) takes in 2 lists of the same length and passes elements with the same index into a binary function f to return a single list find(f, x) returns 1st element of x where f returns true Thursday, April 9,

Functional Programming Example: Hash function def my_hash(mul, key, size): sum = 0 for i in range(len(mul)): sum += mul[i] * key[i] return sum % size Thursday, April 9, def my_hash(mul, key, size): return reduce(plus,zipWith(mul,key,size),0) % size Versus:

57 Questions ?