optimize the len function, to be tail recursive.