Use a tail-recursive version for {Set,Map}.fold

Fix should fix #696