compress using foldr