If you haven't seen this problem before, take a moment to think though it. It's a neat little problem--a close cousin to a lateral thinking puzzle.

Okay, time's up. After a bit of thinking, I ended up with basically four ways of solving the puzzle.

The first idea is to realize that the function didn't necessarily specify that f only be defined for integers, just that all integers must be negated after two applications. So what sort of mathematical object gives a negative after two applications? Imaginary numbers!

Essentially, we use the fact that \( i^2 = -1 \) where \( i = sqrt(-1) \). So if we multiply by \( i \) once, we get \( ni \); twice gives us \( ni^2 = -n \). Perfect:

; Rotate in the complex numbers
(define (f1 n)
(* n 0+1i))

That seems a little unfair though, using complex numbers. Can we do it with only integers?

Of course we can!

To do this, we need to realize that positive/negative is only one axis that splits up the integers. In addition, we could use something like even/odd. This way, we can convert from one to the other (and then back), only applying the negation once.

Specifically, we'll move all even numbers closer from zero. Any odd numbers, we'll negate then move away from zero. That means we'll only negate once:

Alternatively, we could get really tricksy and mess with the range/domain of f even more (just like the complex numbers). For example, we could cast to a string and back, only negating on the way back:

Finally, we can take advantage of the fact that the function will be applied in pairs. So we can add a bit of state to the function. In this example, we create a toggle which is switched on each application of the function. Then, if and only if the toggle is false will we negate the value:

So, do all of these methods work? Well, you could just take my word for it, or you could run some testing. All we care about is double application of integers (single applications and non-integers can return anything), so we'll try to cover the general cases: