############################################################ # Adds all elements of a numeric list. ############################################################ ladd := proc(l:list(numeric)) #----------------------------------------------------------- # Define local variables. #----------------------------------------------------------- local lsum, i: #----------------------------------------------------------- # Check for valid argument, exit with error message # if not valid. #----------------------------------------------------------- if nops(l) = 0 then ERROR(`argument is the NULL list`); fi; #----------------------------------------------------------- # Initialize sum to first element of list. #----------------------------------------------------------- lsum := l[1]; #----------------------------------------------------------- # Loop over rest of elements accumulating the sum. #----------------------------------------------------------- for i from 2 to nops(l) do lsum := lsum + l[i]; od; #----------------------------------------------------------- # Return the sum. #----------------------------------------------------------- lsum; end: