effect of order of function arguments

Josef Svenningsson josefs@cs.chalmers.se
Wed, 19 Feb 2003 14:49:07 +0100 (MET)


On Tue, 18 Feb 2003, Aaron Denney wrote:

> 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. is this:
>
> > interp :: a -> [a] -> [a]
> > interp y   []   =  []
> > interp y (x:[]) = x:[]
> > interp y (x:xs) = x:y:interp y xs
>
> any better than this:
>
> > interp :: [a] -> a -> [a]
> > interp   []   y =  []
> > interp (x:[]) y = x:[]
> > interp (x:xs) y = x:y:interp xs y
>
> Will any implementations notice interp y x:xs calls interp y, and keep
> some sort of interp y partial application around?
>
> (I don't really expect any effect like this, but even if there were one,
> I would expect consideration like the fact that "interp constant" is a
> useful function, while "\x -> interp x const-list" is not so useful to
> outweigh any such effect.  Happily, they don't conflict here.  If there
> is no effect like this, would it make any sense to try to get something
> similar by hand, and can this actually be done?)
>
GHC used to have an optimisation for static argument like this. It would
turn both of the above programs into a similar form using a local
recursive function:

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

GHC doesn't do this anymore. The reason for this is unknown to me.

	/Josef