impl with fold. for append, foldr, foldl.