Branch-and-bound as a higher-order function