File: int_extra.ml

package info (click to toggle)
hol88 2.02.19940316-15
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 65,928 kB
  • sloc: ml: 199,939; ansic: 9,666; sh: 7,118; makefile: 6,075; lisp: 2,747; yacc: 894; sed: 201; cpp: 87; awk: 5
file content (50 lines) | stat: -rw-r--r-- 2,452 bytes parent folder | download | duplicates (11)
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
%****************************************************************************%
% FILE          : int_extra.ml                                               %
% DESCRIPTION   : Additional functions for integer arithmetic in ML.         %
%                                                                            %
% READS FILES   : <none>                                                     %
% WRITES FILES  : <none>                                                     %
%                                                                            %
% AUTHOR        : R.J.Boulton                                                %
% DATE          : 4th March 1991                                             %
%                                                                            %
% LAST MODIFIED : R.J.Boulton                                                %
% DATE          : 12th April 1991                                            %
%****************************************************************************%

%----------------------------------------------------------------------------%
% Absolute value of an integer                                               %
%----------------------------------------------------------------------------%

let abs i = if (i < 0) then (-i) else i;;

%----------------------------------------------------------------------------%
% Function to compute the remainder of an integer division                   %
%----------------------------------------------------------------------------%

ml_curried_infix `mod`;;

let n mod m =
   let r = n - ((n / m) * m)
   in  if (r < 0) then (r + m) else r;;

%----------------------------------------------------------------------------%
% Function to compute the Greatest Common Divisor of two integers.           %
%----------------------------------------------------------------------------%

let gcd (i,j) =
   letrec gcd' (i,j) =
      let rem = i mod j
      in  if (rem = 0)
          then j
          else gcd' (j,rem)
   in (if ((i < 0) or (j < 0))
       then fail
       else if (i < j) then gcd' (j,i) else gcd' (i,j)
      ) ? failwith `gcd`;;

%----------------------------------------------------------------------------%
% Function to compute the Lowest Common Multiple of two integers.            %
%----------------------------------------------------------------------------%

let lcm (i,j) = (i * j) / (gcd (i,j)) ? failwith `lcm`;;