# Practical introduction to Functional Programming with JS

AndreaZanin
47.3K views

## Recursion

A recursive function is a function that calls itself until it hits a base case. This is often used in recursive languages as a replacement for loops, because it is far more flexible. They key here is that each time the function calls itself, the problem is reduced.

Just to understand the concept, let's implement a countdown as a recursive function:

Countdown
let countdown = (num) => {
if (num === 0) { // base case
console.log("BOOM");
}
else {
console.log(num);
countdown(num-1); // recursive call on a reduced problem: num-1
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Recursion is much more powerful than loops; for example, they are very useful to implement any algorithm of the "divide and conquer" family. We'll implement a simple algorithm that tells us whether a file is in a directory or in sub-directories.

Finding a file
/*
OUR FILESYSTEM IS THIS
home
├── andrea
·· ├── funds.csv
·· └── paper.pdf
├── john
·· ├── logs
···· ├── logs1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

In this tiny example, we used recursion, currying, higher order function and lambda expressions, which is nice per se, but most importantly, we solved a problem that is unsolvable using only loops.

And now an exercise for you: from a list of {id,parent} objects you have to create the original structure. You can assume that every object has a unique id.
E.g. this list

[
{ "id": "Grandad", "parent": null },
{ "id": "You", "parent": "Dad" },
{ "id": "Son", "parent": "You" },
{ "id": "Daughter", "parent": "You" },
{ "id": "Brother", "parent": "Dad" },
{ "id": "Nephew", "parent": "Brother" },
{ "id": "Niece", "parent": "Brother" },
{ "id": "Sister", "parent": "Dad" },
{ "id": "Uncle", "parent": "Grandad" },
{ "id": "Cousin", "parent": "Uncle" }
]


should become this

"Grandad": {
"You": {
"Son": {},
"Daughter": {}
},
"Brother": {
"Nephew": {},
"Niece": {}
},
"Sister": {}
},
"Uncle": {
"Cousin": {}
}
}
}


Hint: the subtrees are smaller versions of the treeBuilding problem, can you figure out what the base case is?

Finding a file
const buildTree = (list, parent) => // parent is the root of the tree/subtree
// TODO
// { autofold
module.exports = {
buildTree: buildTree
};
// }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

As this problem is harder that usual I provided the solution here