Question

Hi everyone, I need help in Haskell coding to please only Haskell code, Regards Consider this...

Hi everyone, I need help in Haskell coding to please only Haskell code, Regards

Consider this function:

applyThrice f x = f (f (f x))

this function takes a unary function: for example:

>applyThrice (+2) 2

8

use ., the function composition operator, and/or $, the function application operator, to make this function more readable

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The Haskell language provides us with 2 operators, the function composition operator (.) and the application operator ($).
The two operators can be understood with this simple schematic:

For infix operator $, the left operand should be a function of the type a -> b, the right operand becomes the value on which the function is to be applied. In laymen's terms, if a then get the results for b.
For . both the operands are functions with their return value being a function too. We can use composition to chain many functions to get a final function which accepts the operand, this way, the final function obtained can be used as the left operand for the application operator $.

Now to composite the given function we can proceed as follows:
First looking at the innermost f x:
Here we have a function f which accepts one argument. Since we don't know the type let's assume x has type 'a', so a: x::a.
Thus we can say f accepts a value of type 'a', a: f :: a -> _, let this new unknown type be 'b', thus, we arrive at -
b: f :: a -> b.
So f x would be of type b.

But, we have f(f x) and the input for f is of type ' a'. Thus we can conclude that a and b are actually the same type.
So, we get f:: a -> a. The same can be said for f(f(f x ).

Concurring to this we can say that our functions accept several parameters despite each function actually taking only one parameter and returning partially applied functions until we reach the final function that returns a solid value.

Thus we can rearrange our function for composition as :
applyThrice f = f . f . f $ x

Add a comment
Know the answer?
Add Answer to:
Hi everyone, I need help in Haskell coding to please only Haskell code, Regards Consider this...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT