Type instability of nested recursive function

Thanks! This approach does work, but the actual g in my code uses 5 bindings from the outer function, so moving the inner function outsides would add a lot of arguments.