Understanding exploration in humans and machines by formalizing the function of curiosity