sub¶
Syntax¶
Parameters¶
x- The minuend. An expression that evaluates to a number or a list.
y- The subtrahend. 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.
Description¶
If x and y are single-valued expressions, the action returns the difference of the
two given values (x - y).
If the minuend (the first argument) is a single-valued expression and the subtrahend (the second argument) is a list-expression, then the action returns a list of values consisting of the difference of the single value and each item in the list.
Similarly, when the minuend is a list and the subtrahend is a single value, the operation yields a new list where each element is the result of subtracting the subtrahend from the corresponding element in the original list.
If both arguments are list-expressions, then the action returns a list of values consisting of the difference between the items in the first list and the corresponding elements in the second list. In this case, the lists must have the same number of elements.
This action is the prefix notation for the expression x - y.
If both arguments are list-expressions, a run-time error occurs if the lists have different lengths.
Examples¶
z = sub(3, 1) # z = 1
z = 3 - 1 # z = 1
a_list = list(1, 2, 3)
new_list = sub(a_list, 1) # new_list = list(0, 1, 2)
new_list = a_list - 1 # new_list = list(0, 1, 2)
new_list = 10 - a_list # new_list = list(9, 8, 7)
a_list = list(10, 20, 30)
b_list = list(1, 2, 3)
new_list = a_list - b_list # new_list = list(9, 18, 27)