Cambridge University Press 1.1 Time Clock User Manual


 
3.1. BASIC DATATYPES AND OPERATIONS 21
#let successor x = x + 1;;
successor : int -> int = <fun>
#let o f g = fun x -> f(g x);;
o : (’a -> ’b) -> (’c -> ’a) -> ’c -> ’b = <fun>
#let add3 = o successor (o successor successor);;
add3 : int -> int = <fun>
#add3 0;;
it : int = 3
##infix "o";;
#let add3’ = successor o successor o successor;;
add3’ : int -> int = <fun>
#add3’ 0;;
it : int = 3
It is not possible to specify the precedence of user-defined infixes, nor to make
user-defined non-infix functions right-associative. Note that the implicit opera-
tion of ‘function application’ has a higher precedence than any binary operator,
so successor 1 * 2 parses as (successor 1) * 2. If it is desired to use a func-
tion with special status as an ordinary constant, simply precede it by prefix. For
example:
#o successor successor;;
Toplevel input:
>o successor successor;;
>^
Syntax error.
#prefix o successor successor;;
it : int -> int = <fun>
#(prefix o) successor successor;;
it : int -> int = <fun>
With these questions of concrete syntax out of the way, let us present a system-
atic list of the operators on the basic types above. The unary operators are:
Operator Type Meaning
- int -> int Numeric negation
not bool -> bool Logical negation
and the binary operators, in approximately decreasing order of precedence, are:
Operator Type Meaning
mod int -> int -> int Modulus (remainder)
* int -> int -> int Multiplication
/ int -> int -> int Truncating division
+ int -> int -> int Addition
- int -> int -> int Subtraction
^ string -> string -> string String concatenation
= ’a -> ’a -> bool Equality
<> ’a -> ’a -> bool Inequality
< ’a -> ’a -> bool Less than
<= ’a -> ’a -> bool Less than or equal
> ’a -> ’a -> bool Greater than
>= ’a -> ’a -> bool Greater than or equal
& bool -> bool -> bool Boolean ‘and’
or bool -> bool -> bool Boolean ‘or’