On this page:
3.28.1 Arithmetic Functions
sum
3.28.2 Minimization & Maximization
max
min
arg-max
arg-min
8.11.1

3.28 math

Usage:
include math
import math as ...
The Pyret Math library. It consists of functions for arithmetic calculations, optimization, and more to come!

Every function in this library is available on the math module object. For example, if you used import math as M, you would write M.arg-max to access arg-max below. If you used include, then you can refer to identifiers without writing M. as a prefix.

3.28.1 Arithmetic Functions

sum :: (l :: List<Number>) -> Number

Calculates the arithmetic sum of the Numbers in l. If l contains at least one RoughNum, then the output will be a RoughNum.

Examples:

check: sum([list: ]) is 0 sum([list: 0, 2, 4]) is 6 sum([list: -1, 1, ~2]) is-roughly ~2 end

3.28.2 Minimization & Maximization

max :: (l :: List<Number>) -> Number

Calculates the maximal element of the set of Numbers in l.

Examples:

check: max([list: ]) raises "Empty List" max([list: 10]) is 10 max([list: 2.1, 2, 4.5, ~1.5, -1, 1]) is-roughly 4.5 end

min :: (l :: List<Number>) -> Number

Calculates the minimal element of the set of Numbers in l.

Examples:

check: min([list: ]) raises "Empty List" min([list: 2]) is 2 min([list: -1, 0, ~1, 2, 5]) is-roughly -1 end

arg-max :: (l :: List<Number>) -> Number

Calculates the index of the maximal element within l.

Examples:

check: arg-max([list: ]) raises "Empty List" arg-max([list: 2]) is 0 arg-max([list: -1, 0, ~1, 5, 2]) is 3 end

arg-min :: (l :: List<Number>) -> Number

Calculates the index of the minimal element within l.

Examples:

check: arg-max([list: ]) raises "Empty List" arg-max([list: 8]) is 0 arg-max([list: -1, 0, ~1, -3, 5]) is 3 end