DEV Community

Muhammed H. Alkan
Muhammed H. Alkan

Posted on • Updated on

What is Functional Programming

Firstly, we should know what is a programming paradigm means.
Programming paradigm is a way to classify programming languages.

Let's see the common programming paradigms include

* imperative in which the programmer instructs the machine how to change its state,
  * procedural which groups instructions into procedures,
  * object-oriented which groups instructions together with the part of the state they operate on,

* declarative in which the programmer merely declares properties of the desired result, but not how to compute it
  * functional in which the desired result is declared as the value of a series of function applications, <- We will learn this
  * logic in which the desired result is declared as the answer to a question about a system of facts and rules,
  * mathematical in which the desired result is declared as the solution of an optimization problem

// https://en.wikipedia.org/wiki/Programming_paradigm

When we will functional in any language you should accept these rules:

  • DO NOT mutate your data
    • You should not change the value of data, or change the data to another variable.
    • Use only constants

That means you should disallow side-effects!

// This is not allowed!
let mut something = 20;
something += 7;

// This is allowed.
let something = 20;
let something_else = something + 7;
  • Each function should return value(s).
// This is not allowed!
fn something() {
  println!("27");
}

// This is allowed.
fn something() -> i32 {
  return 5
}

What will can you use when you purely functional:

  • Shadowing
    • Having 2 different constants in different scopes
let a = 20;
{
  let a = 7;
  println!("{}", a);
}
println("{}", a);

/*
Output:
7
20
*/
  • Recursion
    • You can't use for or foreach loops when you purely functional. Because you're just changing the single variables value. This is not allowed because of immutability. Factorial example
fn fact(n: i32) -> i32 {
  if n == 1 {
    1
  }
  else {
    n * fact(n - 1)
  }
}

/*
fact(5);
5 * fact(4)
5 * 4 * fact(3)
5 * 4 * 3 * fact(2)
5 * 4 * 3 * 2 * fact(1) -> It's 1! Return 1 and stop recursion.
5 * 4 * 3 * 2 * 1
5 * 4 * 3 * 2
5 * 4 * 6
5 * 24
120
*/

Foreach-like example (python example :p)

def double_each(arr):
  if arr == []:
    return []
  return [arr[0] * 2] + double_each(arr[1:])

print(double_each([2, 7, 8]))

"""
double_each([2, 7, 8])
[4] + double_each([7, 8]))
[4] + [14] + double_each([8])
[4] + [14] + [16] + double_each([]) -> Got empty list! Return empty list and stop recursion.
[4] + [14] + [16] + []
[4] + [14] + [16]
[4] + [14, 16]
[4, 14, 16]
"""
  • Map, Reduce and Filter
    • Map: apply function f on each item in list and return a new list.
    • Reduce: apply function f to first two items in list and continue like this.
    • Filter: Add to new list if f returns true.

Map

>>> list(map(lambda n: n * 2, [2, 7, 8])
[4, 14, 16]

Reduce

>>> from functools import reduce
>>> reduce(lambda a, b: a + b, [2, 7, 8])
17

Filter

>>> list(filter(lambda n: n % 2 == 0, [2, 7, 8]))
[2, 8]

Questions

  • What is lambda calculus?
    Simply, lambda calculus is a theorical framework for describing functions and their evaluation. It forms the basis of almost all current functional programming languages!
    Wikipedia

  • What is the first functional programming language?
    LISP! The oldest (after FORTRAN (FORmula TRANslation)) and it's still in usage (For example Clojure). It's based on s-expressions.

  • What is Purely Functional?
    Purely functional means ONLY ALLOW FUNCTIONAL PROGRAMMING RULES. However, you can Purely functional in many languages. Just accept the rules.

Made while listening MU40PROJ.KDM by BUILD Engine creator Ken Silverman

This is my first post, so it's possible to make BIG MISTAKES.

Top comments (7)

Collapse
 
yucer profile image
yucer

I guess you were inspired by this post.

I like the graphic that Wikipedia includes for the classification of the paradigms.

Collapse
 
lyfolos profile image
Muhammed H. Alkan • Edited

Actually no, and I have never seen this post.

Also, I commented I got the classification of paradigms from Wikipedia so.

Collapse
 
yucer profile image
yucer

By the way, it is said that programming languages implement many paradigms to different extent.

And the winner multi-paradigm language seems to be Wolfram!

Collapse
 
lyfolos profile image
Muhammed H. Alkan

I find I can call TCO (Tail Call Optimization) as tail-end recursion.

Collapse
 
lyfolos profile image
Muhammed H. Alkan • Edited

I heard it as Tail Recursion. I placed Python as Example, as i can still do it in OCaml. But many people doesn't know OCaml and it's not that popular so.

Note: You're still right.

Collapse
 
lyfolos profile image
Muhammed H. Alkan

As I didn't say it's PURELY FUNCTIONAL, it's still functional.