programming
December 8, 2017

# Language Wars: Round 1 — Functions of Functions Rafał Pocztarski

#### Comparing function definition and function call in different programming languages.

Here we go with the first round of our Language Wars series! This time we’ll be comparing the basic syntax of a function definition and a function call in many different programming languages.

By “functions” I mean functions, methods, procedures, subroutines or maybe even objects or classes, or whatever else makes most sense in a given language — the idea is to compare the basic units of computation using abstractions that are natural to the particular language and to explore the basic means of composition in the most idiomatic constructs to capture the essence of how writing in those languages really feels like.

If you haven’t read it already then you should start from Language Wars: Introduction for some background on what this series is all about:

Language Wars: Introduction

### How We Did It

I wrote the specification below and then me and my teammates from inFullMobile started working on the implementation of the f function in multiple programming languages in (almost) highest secrecy — none of us had seen the other solutions when we started, though we shared them later. Below is what we came up with. We will be discussing each others solutions later in the comments below this article, and we also encourage you to post your comments as well.

The task is simple if not trivial, but it can be implemented in so many different ways that it’s quite useful in demonstrating the programming styles that are most natural to the programming languages used.

#### In Short

We need a function f(a) that for an argument a, such as:

`a: g -> x -> g(g(g(x)))`

returns b, where:

`b: g -> x -> g(g(x))`

i.e. the same as a but with one less g. That’s it.

The solution must work at least for an input with 1, 2, 10, 100 and 1000 g() function invocations — suitable functions for tests are provided below.

#### More Details

We will create a function f that takes a function as an argument and returns another function, so it will be a function of functions or, in other words, a higher-order function if we want to sound more learned. What is the input and output of f is what is most important here.

#### Inputs and Outputs

Let’s say we have a function a that works in a way that whatever function g it is passed as an argument, it returns a function that applies g 3 times:

`a: g -> x -> g(g(g(x)))`

So, for example if g is a function that adds 1 to its argument:

`g: x -> x + 1`

then a(g) is a function that adds 3 to its argument, or more precisely it adds 1 but 3 times:

`a(g): x -> x + 1 + 1 + 1`

If g was doubling its argument:

`g: x -> x × 2`

then a(g) would multiply by 8, i.e. it would double its argument 3 times:

`a(g): x -> x × 2 × 2 × 2`

That is the entire idea of how the input functions will look like.

#### The Solution

Now, our function f should take a function a like this:

`a: g -> x -> g(g(g(x)))`

and return a function b like this:

`b: g -> x -> g(g(x))`

with the only difference that there is one less g.

That’s it — that is everything that we need to implement in this round. It may not sound like an interesting task but it can in fact be implemented in so many different ways using many different programming paradigms that I decided that this will be a perfect task for Round 1.

#### Testing 1, 2, 3

Of course how can we be sure that our implementation works unless we test it properly. We will test our solutions with a function that counts the g invocations inside of functions such as a or b above:

`count: x -> x(inc)(0)`

where a helper function inc just increments its arguments by 1:

`inc: x -> x + 1`

Of course the count function can be implemented in many other different ways but it’s just an example to provide a good starting point.

#### Examples

Below I’ll provide an implementation of some example input functions similar to a and b together with an implementation of the count function, all written in an old JavaScript syntax to make it more readable for everyone and easier to translate to other languages.

We have five functions that we will use for tests of all the implementations:

It’s written in old JavaScript syntax for readability and easier translation to other languages, but of course it can be writtem much more compactly using modern syntax:

As you can see it is easy to compose those functions together which is quite fortunate, because we wouldn’t want to write “g(g(g…” a 1000 times! And we need a functions with 1000 g() invocations to test our implementations properly.

Now, to make sure that the input functions work correctly, we have a count function with the inc helper:

Or the same using modern syntax, with the helper inlined and anonymous:

This is all we need to test our sample input functions:

And this is also all we need to test our f function implementation:

Now, implementing f is just a simple matter of programming…

We will publish the solutions in separate articles. Follow us and stay tuned for the implementations of this round and for more rounds of Language Wars. We would love to hear your opinions — please leave comments below.

Rafał Pocztarski is a Node.js developer at inFullMobile, an international digital product design and development studio based in Warsaw, Poland. Language Wars: Round 1 — Functions of Functions was originally published in inFullMobile Blog on Medium, where people are continuing the conversation by highlighting and responding to this story. Written by Rafał Pocztarski

## You may also like 