[Haskell-beginners] understanding curried function calls

Brent Yorgey byorgey at seas.upenn.edu
Wed Aug 20 20:30:20 UTC 2014


On Wed, Aug 20, 2014 at 02:19:16AM -0600, Dimitri DeFigueiredo wrote:
> 
> doTwice :: (a -> a) -> a -> a
> doTwice f x = f (f x)
> 
> what does this do?
> 
> ex1 :: (a -> a) -> a -> a
> ex1 = doTwice doTwice
> 
> At least, it is clear that there is a parameter to doTwice missing.
> So, I wanted to do:
> 
> ex1 y = (doTwice doTwice) y
> 
> but this gets me nowhere as I don't know how to apply the definition
> of doTwice inside
> the parenthesis without naming the arguments.

Note that function application associates to the left, so

  (doTwice doTwice) y = doTwice doTwice y

So, we have

ex1 y = doTwice doTwice y
      = doTwice (doTwice y)   -- definition of doTwice

Now we are stuck again; we can add another arbitrary parameter.

ex1 y z = doTwice (doTwice y) z
        = (doTwice y) ((doTwice y) z)
        = doTwice y (doTwice y z)   -- remove unnecessary parentheses
        = y (y (doTwice y z))
        = y (y (y (y z)))

Does that help?

> 
> What is the systematic way to evaluate these expressions? I actually
> got really
> stumped when I considered.
> 
> ex2 :: (a -> a) -> a -> a
> ex2 = doTwice doTwice doTwice doTwice
> 
> I assume this is not the same as
> 
> ex2 = (doTwice doTwice doTwice) doTwice

These ARE exactly the same.  It's always the case that

  f w x y z ... = (((f w) x) y) z ...

-Brent


More information about the Beginners mailing list