You are Hercules, about to fight the dreaded Hydra. The Hydra has 9 heads.
When a head is chopped off, it spawns 8 more heads. When one of these 8 heads
is cut off, each one spawns out 7 more heads. Chopping one of these spawns 6
more heads, and so on until the weakest head of the hydra will not spawn out
any more heads.

Our job is to figure out how many chops Hercules needs to make in order to
kill all heads of the Hydra. And no, it’s not n!.

To make it easy to compare both solutions, the data structure I’m using here
is the same one used by Dinkar: a list. In this list, each number represents
a living head and its level of strength.

Now, according to the problem description, when Hercules chops off a level 3
head, the Hydra grows two level 2 heads.

(chop-head(new-hydra3));; => (2 2 3 3)

Here’s one possible implementation for such a function.

(defnchop-head"Returns a new Hydra after chop off its first head."[hydra](let[head(firsthydra)](into(resthydra)(new-hydra(dechead)))))

This code should make sense even if you are not familiar with Clojure.

What happens if Hercules tries to cut off the head of a headless Hydra?

Most functional programming languages I know are laid on top of a strong principle
called the closure property.

In general, an operation for combining data objects satisfies the closure
property if the results of combining things with that operation can
themselves be combined using the same operation. Closure is the key to power
in any means of combination because it permits us to create hierarchical
structures – structures made up of parts, which themselves are made up of
parts, and so on.