Functional Programming — Concepts
In the last article, I wrote the main why's behind functional programming, and explained the main reasons for the hype around functional programming in the last few years. In this article, I'm going to start to formalize some core concepts in functional programming:
Functions
It may sound obvious to start with functions, considering the name of the paradigm(functional), in functional programming functions are more like mathematical functions, and we can do a lot of things with functions like map functions, keep functions in lists, compose functions, combine them and many more. And in general, in functional programming, we only need functions to get something similar to if statements or boolean types.
Let's start with simple functions:
add3 x = x+3
add3 is a function that takes a numeric x value and sums 3 to it
add3 3 --outputs: 6 = (3+3)
add3 0 --outputs: 3 = (0+3)
add3 (-3) --outputs: 0 = (-3+3)
until now, nothing that really goes beyond the basic definition of a mathematical function, so let's add some spice to our functions:
applyFn f v = f v
Now we have a function that receives a function in the f parameter and a value in the v parameter and returns the f called with v:
applyFn add3 3 --outputs 6 (applyFn = add3 3)applyFn (\x -> x+2) 3 --outputs 5
In the previous code example, we introduced two function concepts, the first one is high order functions that consist of passing functions as parameters and using them to generate our final result.
The second concept introduced is a lambda function (\x -> x+2) a lambda function is a function that doesn't need to be declared previously because will only be used once in only a small part of the system, so when we call ‘applyFn (\x -> x+2) 3’ x is replaced by 3 and then three gets added with 2 generating an output of 5.
In mathematics, we can compose functions like:
f(x) = x + 2
g(x) = x * 2 + 1and suppose our x is 5g ∘ ff will be executed first so:
x + 2 -> 5 + 2 -> 7then g will be called with our final result from f that is 7
x * 2 + 1 -> 7 * 2 + 1 -> 14 + 1 -> 15
generating a final output of: 15
In a functional language like haskell composing functions will be very similar to the way we do in mathematics:
f x = x + 2
g x = x * 2 + 1g . f 5
g (f 5)
g 7
15very similar to mathematics, haskell does a great job getting this mathematical correctness
But we also can do a lot of function calls to multiple inputs by keeping functions in lists and processing the list like:
[add3, (*2)] <*> [2,3] -- outputs: [5,6,4,6]
Let's break the code piece above into:
‘[add3, (*2)]’ a list containing functions, our previous add3 function, and an arithmetic function that takes a numeric value and multiplies it by 2
‘<*>’ the apply symbol in the context of applicatives I will not get into much detail about the abstract mathematics of applicatives here, but for now consider the apply symbol as something that takes a list of functions and applies each function to all the values in the list of numbers returning a list of all the values applied to all the functions on the first list
‘[2, 3]’ a usual list of numeric values
And now the output process:
[add3, (*2)]
<*>
[2, 3][add3 2, add3 3] = [5, 6]
[(2*2), (3*2)] = [4, 6]
[5,6] ++(concatenation) [4,6]
[5,6,4,6]
Functions in functional languages offer a lot more options to us, due to functions in functional programming being considered first-class citizens, so I hope you get curious to look for more different approaches to functions, but for this article, I think the applications above make a good example of how powerful and composable functions are and how functions are literally the core of the paradigm
I hope it wasn't a hard to swallow concept for the reader and I hope the reader could understand the examples and the explanation of functions, and again functions are only our first concept in this journey of functional programming, and I also hope to get the reader curious and eager to learn more about functional programming. Thanks for reading until here and see you in the next articles where we will keep talking more about concepts of functional programming.