![]() |
VOOZH | about |
Lambda calculus, developed by Alonzo Church, is a minimal framework for studying computation through functions. It defines what is computable and is equivalent to a Turing machine in power. It offers a theoretical model for describing functions and their evaluation, and forms the foundation of most modern functional programming languages.
Fact: Alan Turing was a student of Alonzo Church who created Turing machine which laid the foundation of imperative programming style. Programming Languages that support functional programming: Haskell, JavaScript, Python, Scala, Erlang, Lisp, ML, Clojure, OCaml, Common Lisp, Racket.
These functions have two main properties.
Programs done using functional programming are easy to debug because pure functions have no side effects or hidden I/O. Pure functions also make it easier to write parallel/concurrent applications. When the code is written in this style, a smart compiler can do many things - it can parallelize the instructions, wait to evaluate results when needing them, and memorize the results since the results never change as long as the input doesn't change.
sum(x, y) // sum is function taking x and y as arguments
return x + y // sum is returning sum of x and y without changing themThere are no “for” or “while” loop in functional languages. Iteration in functional languages is implemented through recursion. Recursive functions repeatedly call themselves, until it reaches the base case.
example of the recursive function:
fib(n)
if (n <= 1)
return 1;
else
return fib(n - 1) + fib(n - 2);In functional programs variables once defined do not change their value throughout the program. Functional programs do not have assignment statements. If we have to store some value, we define new variables instead. This eliminates any chances of side effects because any variable can be replaced with its actual value at any point of execution. State of any variable is constant at any instant.
Example:
x = x + 1 // this changes the value assigned to the variable x.
// So the expression is not referentially transparent. First-class functions are treated as first-class variable. The first class variables can be passed to functions as parameter, can be returned from functions or stored in data structures. Higher order functions are the functions that take other functions as arguments and they can also return functions.
Example:
show_output(f) // function show_output is declared taking argument f
// which are another function
f(); // calling passed function
print_gfg() // declaring another function
print("hello gfg");
show_output(print_gfg) // passing function in another functionIn functional programming, we can’t modify a variable after it’s been initialized. We can create new variables – but we can’t modify existing variables, and this really helps to maintain state throughout the runtime of a program. Once we create a variable and set its value, we can have full confidence knowing that the value of that variable will never change.
Fact: Whatsapp needs only 50 engineers for its 900M users because Erlang is used to implement its concurrency needs.
Facebook uses Haskell in its anti-spam system. Functional programming matters because it offers several benefits that align well with modern computing needs:
Functional programming is supported by several languages, each with unique features:
Object-Oriented Programming (OOP) and Functional Programming (FP) represent different approaches to software design:
Aspect | Object-Oriented Programming (OOP) | Functional Programming (FP) |
|---|---|---|
Focus | Encapsulates state within objects. State is mutable and can be changed by methods. | Encapsulates state within objects. State is mutable and can be changed by methods. |
State Management | Encapsulates state within objects. State is mutable and can be changed by methods. | Encapsulates state within objects. State is mutable and can be changed by methods. |
Modularity | Achieved through classes and objects; methods define behavior, attributes define state. | Achieved through pure functions and function compositions; data is passed between functions. |
Data Handling | Data and behavior are bundled together in objects; state changes occur through methods. | Data is immutable and managed through function applications. |
Code Reusability | Achieved through inheritance and polymorphism; classes can be extended and methods overridden. | Achieved through higher-order functions; functions can be passed as arguments and returned from other functions. |