I'm learning FP and have a few confusion after playing around with GHCi.
Say I have 2 simple functions:
twice :: (a -> a) -> (a -> a)
twice f a = f (f a) -- Equation 1
double :: Int -> Int
double = \x -> x * 2
Decomposing the evaluation twice twice twice double 3 (note that 3xtwice+1xdouble), I would have:
{-
twice twice twice double 3
== (twice twice twice double) 3
== (twice twice (twice double)) 3
== (twice (twice (double double 3)))
== (twice ((double double) (double double 3)))
== (((double double) (double double)) ((double double) (double double 3)))
== 768
-}
- Is this correct?
- According to this, if my definition of
twiceis changed astwice f a = f f a -- Equation 2, I should decompose the evaluation, with left associativity, as:
{-
twice (twice twice double) 3
== (twice twice double) (twice twice double) 3
== ((twice double)(twice double)) ((twice double)(twice double)) 3
== ((double double)(double double)) ((double double)(double double)) 3
== (double (double (double (double (double (double (double (double 3 ) ) ) ) ) ) )
== 768
-}
right?
- However, the strangest part is GHC REPL gave me the answer of
196608(2^16*3):
> twice twice twice double 3
196608
which then makes me so confusing. Where would I make the mistake? Thanks.