[Haskell-beginners] understanding curried function calls

Brent Yorgey byorgey at seas.upenn.edu
Thu Aug 21 16:43:28 UTC 2014


Hi Dimitri,

Yes, this is one possible systematic approach, which will always work.
And it's the one I happen to prefer.  (Another approach is to turn
everything into lambdas, as pointed out earlier in this thread, but
that is noisier and does not correspond to the usual Haskelly way of
defining functions.)  I am not sure exactly what you are trying to do
with the foldl/foldr example, but you should be able to use this
same approach to evaluate it.

-Brent

On Wed, Aug 20, 2014 at 03:11:22PM -0600, Dimitri DeFigueiredo wrote:
> Hi Brent,
> 
> Is this how we go about solving these then?
> Keep adding parameters on the right until we have enough of them that
> we are able to apply the function definition.
> 
> In other words, in
> ex1 = doTwice doTwice
> 
> the first doTwice needs another parameter, so you added 'y'.
> ex1 y = (doTwice doTwice) y = doTwice doTwice y
> 
> Then we can apply the definition.
> We got stuck again, so you added another parameter 'z'.
> Then again applied the definition of the left most function.
> And so on.
> 
> Is there a similarly systematic approach for the foldl implemented by
> foldr example?
> I'm looking for systematic approaches that I can then practice.
> 
> 
> Thanks!
> 
> 
> Dimitri
> 
> 
> Em 20/08/14 14:30, Brent Yorgey escreveu:
> >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
> >_______________________________________________
> >Beginners mailing list
> >Beginners at haskell.org
> >http://www.haskell.org/mailman/listinfo/beginners
> 
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners


More information about the Beginners mailing list