Years ago we wrote a simple article explaining differences between function composition in Elixir and Javascript.

Now it’s 2002 and the chat-GPT3 bot is out, so we decided to test it with the following task:

“what is the best functional language in terms of flexibility regarding as example function composition or currying. Explain differences between the mentioned languages with code examples”

The result is below… no edit from us. Really impressive…

It is difficult to determine which functional programming language is the “best” in terms of flexibility, as this can vary depending on the specific needs and requirements of a given project. However, some functional programming languages are known for their support for features such as function composition and currying, which can provide greater flexibility when writing functional code.

Function composition is the process of combining multiple functions to create a new function, and currying is the process of converting a function that takes multiple arguments into a series of functions that each take a single argument. Both of these features can make it easier to write modular, reusable code.

Some functional programming languages that are known for their support for function composition and currying include Haskell, Elixir, and F#. Here is an example of function composition and currying in each of these languages:

Haskell:


-- Define some functions
addOne x = x + 1
multiplyByTwo x = x * 2
subtractThree x = x - 3

-- Create a new function by composing the above functions
addOneMultiplyByTwoSubtractThree = subtractThree . multiplyByTwo . addOne

-- Apply the new function to an input
addOneMultiplyByTwoSubtractThree 4
-- Output: 5

Elixir:


# Define some functions
def add_one(x) do
x + 1
end

def multiply_by_two(x) do
x * 2
end

def subtract_three(x) do
x - 3
end

# Create a new function by composing the above functions
add_one_multiply_by_two_subtract_three = compose(
&add_one/1,
&multiply_by_two/1,
&subtract_three/1
)

# Apply the new function to an input
add_one_multiply_by_two_subtract_three.(4)
# Output: 5

F#:

// Define some functions
let addOne x = x + 1
let multiplyByTwo x = x * 2
let subtractThree x = x - 3

// Create a new function by composing the above functions
let addOneMultiplyByTwoSubtractThree = subtractThree >> multiplyByTwo >> addOne

// Apply the new function to an input
addOneMultiplyByTwoSubtractThree 4
// Output: 5

In each of these examples, the addOneMultiplyByTwoSubtractThree function is created by composing the addOne, multiplyByTwo, and subtractThree functions. When this function is applied to the input 4, it applies the addOne function to the input, resulting in 5, then applies the multiplyByTwo function to the result, resulting in 10, and finally applies the subtractThree function to the result, resulting in the final output of 5.

All of these languages provide support for function composition and currying, but they have some differences in the syntax and approach used. For example, in Haskell, the . operator is used to combine functions in a composition, while in Elixir, the compose function is used, and in F#, the >> operator is used. Additionally, the way that currying is implemented can vary between these languages.