Download First-Class Functions What is functional programming? First

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

C Sharp (programming language) wikipedia , lookup

Lambda calculus definition wikipedia , lookup

Lambda calculus wikipedia , lookup

Combinatory logic wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Currying wikipedia , lookup

Closure (computer programming) wikipedia , lookup

Lambda lifting wikipedia , lookup

Anonymous function wikipedia , lookup

Standard ML wikipedia , lookup

Transcript
9/9/15
What is f unctional programming?
“Functional programming” can mean a few different things:
1. Avoiding mutation in most/all cases (done and ongoing)
2. Using functions as values (this unit)
… • Style encouraging recursion and recursive data structures
• Style closer to mathematical definitions
• Programming idioms using laziness (later topic, briefly)
• Anything not OOP or C? (not a good definition)
First-­Class Functions
Not sure a definition of “functional language” exists beyond “makes functional programming easy / the default / required”
– No clear yes/no for a particular language
2
adapted from materials by Dan Grossman a t the U niversity o f W ashington
First-­class functions
Function Closures
Functions are first-­class values, can be used wherever we use values:
– Arguments to function calls
– Results of function bodies
– Parts of cons cells
– Bound to variables
Sneak peak:
– Function bodies can use bindings from outside the function definition (in scope where function is defined)
– Distinct concept from first-­class functions.
– Back to this powerful idea soon!
– …
Common!
– Other function is called a higher-­order function
– Powerful way to factor out common functionality
3
4
1
9/9/15
Onward
Functions as arguments
– How to use first-­class functions and closures
– The precise semantics
– Multiple powerful idioms
We can pass one function as an argument to another function
– Not a new feature!
(define (do-to-both f pair)
(cons (f (car pair)) (f (cdr pair))))
Elegant strategy for factoring out common code.
Combines well with anonymous functions.
[See separate code examples]
5
A s tyle point
6
Map
Compare:
(define (map f xs)
(if (null? xs)
null
(cons (f (car xs)) (map f (cdr xs)))))
(if x #t #f)
With:
(lambda (x) (f x))
So don’t do this:
(n-times (lambda (x) (cdr x)) 2 (list 1 2 3 4))
When you can do this:
(n-times cdr 2 (list 1 2 3 4))
Map is, without a doubt, in the “higher-­order function hall of fame”
– Standard name (for any data structure)
– You use it all the time once you know it:
• Saves space.
• Clearly communicates intent.
– Predefined, but just a function.
14
15
2
9/9/15
Filter
Generalizing
(define (filter f xs)
(if (null? xs)
null
(if (f (car xs))
(cons (car xs) (filter f (cdr xs)))
(filter f (cdr xs)))))
Our examples of first-­class functions so far all:
– Take one function as an argument to another function
– Process a number or a list
But first-­class functions are useful anywhere for any kind of data
–
–
–
–
Filter is also in the hall of fame
– So use it whenever your computation is a filter.
– Predefined, but just a function.
Can pass several functions as arguments
Can put functions in data structures (tuples, lists, etc.)
Can return functions as results
Can write higher-­order functions that traverse your own data structures
Useful whenever you want to abstract over “what to compute with”
– No new language features
16
17
Returning functions
Remember: Functions are first-­class values
– For example, can return them from functions
Silly example:
(define (double-or-triple-if-f-of-seven f)
(if (f 7)
(lambda (x) (* 2 x))
(lambda (x) (* 3 x))))
18
3