Can someone please help me with the below,
applyTwice :: (a -> a) -> a -> a
applyTwice f x = f (f x)
I do not understand how the above works. If we had something like ( 3) 10
surely it would produce 13
? How is that f (f x)
. Basically I do not understand currying when it comes to looking at higher order functions.
So what I'm not understanding is if say we had a function of the form a -> a -> a
it would take an input a
then produce a function which expects another input a to produce an output. So if we had add 5 3
then doing add 5
would produce a function which would expect the input 3
to produce a final output of 8
. My question is how does that work here. We take a function in as an input so does partial function application work here like it did in add x y
or am I completely overcomplicating everything?
CodePudding user response:
That's not currying, that's partial application.
> :t ( )
( ) :: Num a => a -> a -> a
> :t ( ) 3
( ) 3 :: Num a => a -> a
The partial application ( ) 3
indeed produces a function ( 3)
(*) which awaits another numerical input to produce its result. And it does so, whether once or twice.
You example is expanded as
applyTwice ( 3) 10 = ( 3) (( 3) 10)
= ( 3) (10 3)
= (10 3) 3
That's all there is to it.
(*)(actually, it's (3 )
, but it's the same as ( 3)
anyway)