add foldr/build optimisation for take and replicate