wrote function generate-loop