1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
_S_e_c_t_i_o_n_s
An infix operator can be partially applied, by supplying it with only
one of its operands, the result being a function of one argument. Such
expressions are always parenthesised, to avoid ambiguity, and are called
`sections'. Two different kinds of sections (called presections and
postsections) are possible since either the first or the second operand
can be supplied.
An example of a presection is
(1/)
which denotes the reciprocal function. An example of a postsection is
(/3)
which gives a concise notation for the `divide by three' function. Note
that both of these examples are functions of type (num->num). With one
exception (see below) sections can be formed using any infix operator.
Further examples are (0:) which is a function for prefixing lists of
numbers with zero, and (^2) which is the square function.
Sections may be regarded as the analogue of currying for infix
operators. They are a minor syntactic convenience, and do not really
add any power to the language, since any function denoted in this way
could have been introduced by explicit definition. For the first two
examples given above we could have written, say
reciprocal y = 1/y
divide_by_three x = x/3
and then used the function names, although this would have been somewhat
more verbose.
To summarise the conventions for infixes, taking infix division as an
example, note that the following expressions are all equivalent.
a / b
(/) a b
(a /) b
(/ b) a
The usual rules about operator precedence (see manual section on
operators) apply to sections. For example we can write (a*b+) but not
(a+b*), because `*' is more binding than `+'. The latter example should
have been written ((a+b)*). As always when writing complicated
expressions, if there is any possibility of ambiguity it is better to
put in extra parentheses.
_S_p_e_c_i_a_l_ _c_a_s_e
It is not possible to form a postsection in infix minus, because of a
conflict of meaning with unary minus. For example:
(-b)
is a parenthesised occurrence of negative b, not a section. As a way
round this there is a function `subtract' in the standard environment
with the definition:- subtract x y = y - x. This is a normal curried
function, so we can write (subtract b) to get the function that
subtracts b from things.
Presections in infix minus, such as (a-), cause no ambiguity. There are
no problems with infix plus because Miranda does not have a unary plus
operator.
_A_c_k_n_o_w_l_e_d_g_e_m_e_n_t:
The idea of sections is due to Richard Bird (of Oxford University
Programming Research Group) and David Wile (of USC Information Sciences
Institute).
|