Added a tail-recursive fold function for testing purposes.