A function is a rule that, for each element in some set A of inputs, assigns an output chosen from set B, but without necessarily using every member of B. The domain is a set from which the function’s input values are chosen. The co-domain is a set from which the function’s output values are chosen. The domain and co-domain are subsets of objects in some data type.
In functional languages, a function is a first-class object. This means that it can be an argument to another function as well as the result of a function call.
First-class objects are objects which can:
A higher-order function takes a function as an argument or returns a function as a result, or does both.
A function f has a function type f: A -> B, where A -> B denotes the type of a function that takes an argument type A and outputs a result type B.
Partial function application is where some arguments of a multi-argument function are fixed, giving a new function with less arguments. For example, consider the function add that takes two integers and adds them. This gives add: Integer -> (Integer -> Integer). Partial function application means that add 4 is a new function that when applied to an integer, returns that integer plus 4.
The brackets can be removed to give add: Integer -> Integer -> Integer. The function add is now viewed as taking one argument after another and returning a result of data type integer.
The operation functional composition combines two functions to get a new function. Given two functions f: A -> B and g: B -> C, the function g ○ f, called the composition of g and f, is a function whose domain is A and co-domain is C. The function on the right is applied first.
A list has a head and a tail, where the head is the first item in the list, and a tail is the remainder of the list, written as head:tail in Haskell. The head is a list element, while the tail is a list itself. 4:[3, 5] would represent a list with head 4 and tail [3, 5]. A list can also be empty.
Functions in Haskell:
add :: Integer -> Integer -> Integer
add x y = x + y
add 6 7 -- outputs 13
Partial function application in Haskell:
add :: Integer -> Integer -> Integer
add x y = x + y
add3 :: Integer -> Integer
add3 = add 3
add3 4 -- outputs 7
Function composition:
add3 :: Integer -> Integer
add3 x = x + 3
square :: Integer -> Integer
square x = x * x
(add3 . square) 8 -- outputs 67
Map:
triple :: Integer -> Integer
triple x = 3 * x
map triple [1, 2, 3] -- outputs [3, 6, 9]
Filter:
isBig :: Integer -> Bool
isBig x = x >= 5
filter isBig [2, 4, 6, 8] -- outputs [6, 8]
Fold:
multiply :: Integer -> Integer -> Integer
multiply x y = x * y
foldr multiply [1, 2, 3, 4] -- outputs 24
just for fun, a python widget!
print("hello, world!")