6.16.1 Context-dependent functions.
The + operator
The + operator is infixed and ’+’ is its prefixed
version. The + operator will add numbers (see
Section 6.8.2), concatenate strings (see
Section 6.3.12), and convert a number to a string if
necessary. Addition makes sense for other objects, and + can
flexibly deal with them; the result of using the + operator
depends on the nature of its arguments.
Examples.
-
Input:
1+2+3+4
or:
’+’(1,2,3,4)
or:
(1,2)+(3,4)
or:
(1,2,3)+4)
Output:
(See Section 6.39.9.)
- Input:
1+i+2+3*i
or:
’+’(1,i,2,3*i)
Output:
- Input:
[1,2,3]+[4,1]
or:
[1,2,3]+[4,1,0]
or:
’+’([1,2,3],[4,1])
Output:
- Input:
[1,2]+[3,4]
or:
’+’([1,2],[3,4])
Output:
- Input:
[[1,2],[3,4]]+[[1,2],[3,4]]
Output:
- Input:
[1,2,3]+4
or:
’+’([1,2,3],4)
Output:
(This is a polynomial; see Section 6.27.1.)
- Input:
[1,2,3]+(4,1)
or:
’+’([1,2,3],4,1)
Output:
- Input:
"Hel"+"lo"
or:
’+’("Hel","lo")
Output:
"Hello"
The -,* and / operators
The -, * and / operators (and their
prefixed versions ’-’, ’*’ and ’/’), like
the + operator, are flexible and operate on compound objects
(such as lists and sequences), but don’t concatenate strings.
Examples of - and ’-’.
-
Input:
(1,2)-(3,4)
Output:
- Input:
(1,2,3)-4
Output:
- Input:
[1,2,3]-[4,1]
or:
[1,2,3]-[4,1,0]
or:
’-’([1,2,3],[4,1])
Output:
- Input:
[1,2]-[3,4]
or:
’-’([1,2],[3,4])
Output:
- Input:
[[3,4],[1,2]]-[[1,2],[3,4]]
Output:
- Input:
[1,2,3]-4
or:
’-’([1,2,3],4)
Output:
- Input:
[1,2,3]-(4,1)
Output:
Examples of * and ’*’.
-
Input:
(1,2)*(3,4)
or:
(1,2,3)*4
or:
1*2*3*4
or:
’*’(1,2,3,4)
Output:
- Input:
1*i*2*3*i
or:
’*’(1,i,2,3*i)
Output:
- Input:
[10,2,3]*[4,1]
or:
[10,2,3]*[4,1,0]
or:
’*’([10,2,3],[4,1])
Output:
These compute the scalar product.
- Input:
[1,2]*[3,4]
or:
’*’([1,2],[3,4])
Output:
These compute the scalar product.
- Input:
[[1,2],[3,4]]* [[1,2],[3,4]]
Output:
- Input:
[1,2,3]*4
or:
’*’([1,2,3],4)
Output:
- Input:
[1,2,3]*(4,2)
or:
’*’([1,2,3],4,2)
or:
[1,2,3]*8
Output:
- Input:
(1,2)+i*(2,3)
or:
1+2+i*2*3
Output:
Examples of / and ’/’.
-
Input:
[10,2,3]/[4,1]
Output:
- Input:
[1,2]/[3,4]
or:
’/’([1,2],[3,4])
Output:
- Input:
1/[[1,2],[3,4]]
or:
’/’(1,[[1,2],[3,4]]
Output:
- Input:
[[1,2],[3,4]]*1/ [[1,2],[3,4]]
Output:
- Input:
[[1,2],[3,4]]/ [[1,2],[3,4]]
Output:
(This is term-by-term division.)