[Haskell-cafe] Trying to implement this code: flipping question

Malcolm Wallace Malcolm.Wallace at cs.york.ac.uk
Tue Apr 19 05:56:58 EDT 2005

Jon Fairbairn <Jon.Fairbairn at cl.cam.ac.uk> writes:

> Which leads me to my own question: in current
> implementations, if I have a data structure containing a
> function (D f, say), and from it I generate anoter (D (flip
> f)), and then later (D (flip (flip f))) and so on, do the
> flips get reduced (to nothing if there's an even number of
> them) when the field of D is evaluated?

No, not as far as I can see, unless there are opportunities for static
inlining (and therefore compile-time reduction).  AIUI, the application
of flip needs to see three arguments before it can be evaluated.
Thus, in this example:

    let f''  = flip (flip f)
        appf = f'' 0
    in [appf 1, appf 2, appf 3]

the code for flip will run six times, whereas here:

    let f''  = flip (flip f)
        appf = f'' 0 1
    in [appf 2, appf 3, appf 4]

the code for flip is run only twice.  The difference in the examples
is that flip is only partially applied in the first case, but fully
saturated in the second.

At any rate, I think this is how it works in nhc98.


More information about the Haskell-Cafe mailing list