Prevent stack overflow with tail-recursive map

Without this change, a stack overflow happens when packing a 10M long
list of char. The fix is to replace calls to List.map with a
tail-recursive version. Test case included.