Search code examples
haskellpartial-application

Haskell partial applications to add 1 and double


I have to write a program that takes in an Integer and then uses two partial applications to first increment the number by one and then the second partial application doubles it. I know that a partial application would be one that would take less arguments than specified but each of these partial applications only needs 1 input. Does that mean I'm not passing anything into either of the partial applications? That really seems wrong/weird to me. Any help on where to start on the partial applications would be greatly appreciated


Solution

  • It sounds like you are supposed to use functions that take two arguments and partially apply them to arrive with the functions that take one argument. To increment a number by one using partial application, you might do something like this:

    add a b = a + b
    add_one = add 1
    

    You take a function to add two numbers and partially apply it with one, so now you have a function that adds one to a number. The same principle applies to doubling.

    multiply a b = a * b
    double = multiply 2
    

    The doubling function is just 2 partially applied to multiplication. To combine these, you can use function composition:

    doubleIncr = multiply 2 . add 1
    

    Hope this helps!