previous | index | next

Functions As Arguments

A straightforward translation from the Racket version:

bool increasingOnIntegerRange(int f(int), int low, int high) {
    if ( low == high )
        return true;
    else if ( f(low) < f(low+1) )
        return increasingOnIntegerRange(f, low+1, high);
    else
        return false;
}

The following assertions will all succeed:

    assert( increasingOnIntegerRange(square, 0, 6) );
    assert( !increasingOnIntegerRange(square, -3, 3) );

    assert( increasingOnIntegerRange(identity, 0, 6) );
    assert( increasingOnIntegerRange(identity, -3, 3) );
Note that ! is the negation operator in C++.

!expression is the same as (not expression) in Racket.


previous | index | next