Module Integer

A collection of common operations on integer numbers. Most operations make no assumption on the precision of integers. Operation bitNot is necessarily an exception.

Author: Sergio Antoy

Version: October 2016

Summary of exported operations:

(^) :: Int -> Int -> Int   
The value of a ^ b is a raised to the power of b.
pow :: Int -> Int -> Int   
The value of pow a b is a raised to the power of b.
ilog :: Int -> Int   
The value of ilog n is the floor of the logarithm in the base 10 of n.
isqrt :: Int -> Int   
The value of isqrt n is the floor of the square root of n.
factorial :: Int -> Int   
The value of factorial n is the factorial of n.
binomial :: Int -> Int -> Int   
The value of binomial n m is n*(n-1)*...*(n-m+1)/m*(m-1)*...1.
max3 :: Ord a => a -> a -> a -> a   
Returns the maximum of the three arguments.
min3 :: Ord a => a -> a -> a -> a   
Returns the minimum of the three arguments.
maxlist :: Ord a => [a] -> a   
Returns the maximum of a list of integer values.
minlist :: Ord a => [a] -> a   
Returns the minimum of a list of integer values.
bitTrunc :: Int -> Int -> Int   
The value of bitTrunc n m is the value of the n least significant bits of m.
bitAnd :: Int -> Int -> Int   
Returns the bitwise AND of the two arguments.
bitOr :: Int -> Int -> Int   
Returns the bitwise inclusive OR of the two arguments.
bitNot :: Int -> Int   
Returns the bitwise NOT of the argument.
bitXor :: Int -> Int -> Int   
Returns the bitwise exclusive OR of the two arguments.
even :: Int -> Bool   
Returns whether an integer is even
odd :: Int -> Bool   
Returns whether an integer is odd

Exported operations:

(^) :: Int -> Int -> Int   

The value of a ^ b is a raised to the power of b. Fails if b < 0. Executes in O(log b) steps.

Example call:
(a ^ b)
Parameters:
  • a : The base.
  • b : The exponent.
Returns:
a raised to the power of b.
Further infos:
  • defined as right-associative infix operator with precedence 8

pow :: Int -> Int -> Int   

The value of pow a b is a raised to the power of b. Fails if b < 0. Executes in O(log b) steps.

Example call:
(pow a b)
Parameters:
  • a : The base.
  • b : The exponent.
Returns:
a raised to the power of b.

ilog :: Int -> Int   

The value of ilog n is the floor of the logarithm in the base 10 of n. Fails if n <= 0. For positive integers, the returned value is 1 less the number of digits in the decimal representation of n.

Example call:
(ilog n)
Parameters:
  • n : The argument.
Returns:
the floor of the logarithm in the base 10 of n.

isqrt :: Int -> Int   

The value of isqrt n is the floor of the square root of n. Fails if n < 0. Executes in O(log n) steps, but there must be a better way.

Example call:
(isqrt n)
Parameters:
  • n : The argument.
Returns:
the floor of the square root of n.

factorial :: Int -> Int   

The value of factorial n is the factorial of n. Fails if n < 0.

Example call:
(factorial n)
Parameters:
  • n : The argument.
Returns:
the factorial of n.

binomial :: Int -> Int -> Int   

The value of binomial n m is n*(n-1)*...*(n-m+1)/m*(m-1)*...1. Fails if m <= 0 or n < m.

Example call:
(binomial n m)
Parameters:
  • n : Argument.
  • m : Argument.
Returns:
the binomial coefficient of n over m.

max3 :: Ord a => a -> a -> a -> a   

Returns the maximum of the three arguments.

Example call:
(max3 n m p)
Parameters:
  • n : Argument.
  • m : Argument.
  • p : Argument.
Returns:
the maximum among n, m and p.

min3 :: Ord a => a -> a -> a -> a   

Returns the minimum of the three arguments.

Example call:
(min3 n m p)
Parameters:
  • n : Argument.
  • m : Argument.
  • p : Argument.
Returns:
the minimum among n, m and p.

maxlist :: Ord a => [a] -> a   

Returns the maximum of a list of integer values. Fails if the list is empty.

Example call:
(maxlist l)
Parameters:
  • l : The list of values.
Returns:
the maximum element of l.
Further infos:
  • partially defined

minlist :: Ord a => [a] -> a   

Returns the minimum of a list of integer values. Fails if the list is empty.

Example call:
(minlist l)
Parameters:
  • l : The list of values.
Returns:
the minimum element of l.
Further infos:
  • partially defined

bitTrunc :: Int -> Int -> Int   

The value of bitTrunc n m is the value of the n least significant bits of m.

Example call:
(bitTrunc n m)
Parameters:
  • n : Argument.
  • m : Argument.
Returns:
m truncated to the n least significant bits.

bitAnd :: Int -> Int -> Int   

Returns the bitwise AND of the two arguments.

Example call:
(bitAnd n m)
Parameters:
  • n : Argument.
  • m : Argument.
Returns:
the bitwise and of n and m.

bitOr :: Int -> Int -> Int   

Returns the bitwise inclusive OR of the two arguments.

Example call:
(bitOr n m)
Parameters:
  • n : Argument.
  • m : Argument.
Returns:
the bitwise inclusive or of n and m.

bitNot :: Int -> Int   

Returns the bitwise NOT of the argument. Since integers have unlimited precision, only the 32 least significant bits are computed.

Example call:
(bitNot n)
Parameters:
  • n : Argument.
Returns:
the bitwise negation of n truncated to 32 bits.

bitXor :: Int -> Int -> Int   

Returns the bitwise exclusive OR of the two arguments.

Example call:
(bitXor n m)
Parameters:
  • n : Argument.
  • m : Argument.
Returns:
the bitwise exclusive of n and m.

even :: Int -> Bool   

Returns whether an integer is even

Example call:
(even n)
Parameters:
  • n : Argument.
Returns:
whether n is even.

odd :: Int -> Bool   

Returns whether an integer is odd

Example call:
(odd n)
Parameters:
  • n : Argument.
Returns:
whether n is odd.