effect of order of function arguments

Jan-Willem Maessen jmaessen@MIT.EDU
Wed, 19 Feb 2003 10:32:13 -0500


Aaron Denney <wnoise@ofb.net> writes:
> With a recursive function of more than one argument, does it make sense
> to keep the arguments that tend to remain constant closer to the front?
>
> i.e.
>
> Will any implementations notice interp y x:xs calls interp y, and keep
> some sort of interp y partial application around?

Systems which perform lambda-lifting will usually be tuned this way,
as the same optimization also speeds up tail recursion.  For example:

interp y xs = interpaux xs
  where interpaux [] = ...
        interpaux (x:xs) = ... interpaux xs ...

Will be lambda-lifted to the original interp definition, with an extra
call:

interp y xs = interpaux y xs

interpaux y [] = ...
interpaux y (x:xs) = ... interpaux y xs ...

This one of the reasons nhc does this optimization, I'm sure.  I know
the Eager Haskell compiler avoids pushing and popping the invariant
arguments from the stack during a tail call; I recall that hbc does so
as well.

-Jan-Willem Maessen
Eager Haskell Project
jmaessen@mit.edu