Exercise 2: Function composition

Function composition

Defining functions by combining others is interesting, isn't it?

Earlier, we saw a case where a big function is just applying a "small" function and then applying another "small" function to its result:

nextTimesTwo number = timesTwo (next number)

This idea of passing the result of a function to the input of another is so common on functional programming that it has an own name: composition. We say that nextTimesTwo is the composition between timesTwo and next. Then, we can write it in a shorter way:

nextTimesTwo = timesTwo.next

Ok, let's see if you understand it: write a function named previousTimesThree, that is timesThree composed with previous. Assume timesThree and previous are already defined.

You must sign in before submitting your solutions

Oops, your solution didn't work

  • Check that your program does not have recursion or an infinite loop
  • Check that you have an internet connection
  • Wait a while and try again

We are processing you solution

Please press F5 if results are not displayed after a few seconds