add()
Returns the result of adding two values or adding a value to each item in a list or adding pairwise the items in two lists.
Synopsis
add(x, y)
Description
If x
and y
are single-valued expression, the function returns
the sum of the two given values.
If one the arguments is a single-valued expression and the other is a list-expression, then the function returns is a list of values consisting of the sum of the single value and each item in the list.
If both arguments are list-expressions, then the function returns is a list of values consisting of the sum of the corresponding items in each list. In this case, the lists must have the same number of items.
This function is the prefix notation for the expression x + y
.
Parameters
x
An expression that evaluates to a number or a list.
y
An expression that evaluates to a number or a list.
Returns
The result is a single value if both arguments are single-valued expressions; otherwise the result is a list.
Errors
If both arguments are list-expressions, a run-time error occurs if the lists have different lengths.
Examples
z = add(1, 2) # z = 3
z = 1 + 2 # z = 3
my_list = list(1, 2, 3)
new_list = add(my_list, 4) # new_list = list(5, 6, 7)
new_list = my_list + 4 # new_list = list(5, 6, 7)
new_list = 4 + my_list # new_list = list(5, 6, 7)
new_list = my_list + my_list # new_list = list(2, 4, 6)