Functional Programming Fundamentals Flashcards
Functional programming
A function is a self contained statement that returns a value when given all required inputs and executed
They are the building blocks of a program in functional programming
Given the same input the function will always return the same output without altering any other data - to achieve this data in functional programs is immutable; if results need to be stored, then new data structures need to be made rather than updating previously used variables
Functional programs need to be stateless, meaning they dont remember anything that has happened before
Functions
A function is a rule that, for each element in some set A of inputs, assigns an output chosen from set B without using every member of set B
The set from which inputs are chosen is the domain; the set from which outputs are calculated is the co-domain
eg. F(x) = X^2, where x is the domain and x^2 is the co-domain
All functions have a function type defined as
- F: A → B, where F is the function, A is the argument type and input and B is the result type and output of the function
The domain and co-domain are always subsets of objects in some data type
First class objects
A first class object is an object which may:
- Appear in an expression
- Be assgines to a variable
- Be assigned as arguments to a function
- Be returned by a function call
Functions are also first class objects in functional programming languages
Function application
Giving particular inputs to a function is called function application
eg. Square 3, where Square: integer → integer, would be an application of the argument 3 to the Square function
Partial function application is where it is possible to call a function with fewer arguments than needed - some of the arguments are fixed and can be stored as a variable for later use
This is possible because functions are first class objects
Function Composition
Functional composition is the act of combining two functions to create a new function.
The benefit of this is that the user is able to use the functions both separately, and in
conjunction.
Any two functions can be combined as long as the domain of one of the
functions is the same as the co-domain of the other