Constants
(constant) math_e :float
The Number value for e, Euler's number,
which is approximately 2.718281828459045.
Type:
- float
(constant) math_inf :float
The name inf refers to float value positive infinity.
(For negative infinity, use -math.inf.)
See also Python 3.13 Documentation.
Type:
- float
(constant) math_nan :float
A floating-point “not a number” (nan) value.
See also Python 3.13 Documentation.
Type:
- float
(constant) math_pi :float
The float value of
π,
the ratio of the circumference of a circle to its diameter,
which is approximately 3.1415926535897932.
Type:
- float
(constant) math_tau :float
Tau is a circle constant equals to
2π,
the ratio of a circle’s circumference to its radius,
which is approximately 6.283185307179586.
Type:
- float
Functions
abs(x) → {int|float|complex}
Return the absolute value of
x.
For an int input, it returns the non-negative integer equivalent.
For a float input, it returns the positive floating-point number representing its magnitude.
For a complex input, it computes and returns the modulus (the square root of the sum of the
squares of the real and imaginary parts) as a float.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | complex | The number whose absolute value is computed. |
Returns:
the absolute value of
x
- Type
- int | float | complex
append(xs, ys) → {linked_list}
Returns a linked list that results from appending the linked list ys to the linked list xs.
Parameters:
| Name | Type | Description |
|---|---|---|
|
linked_list | given linked list |
|
linked_list | given linked list |
Returns:
linked list that results from appending ys to xs
- Type
- linked_list
arity(f) → {int}
Returns the number of parameters the given function
f expects, excluding the rest parameter.
Parameters:
| Name | Type | Description |
|---|---|---|
|
function | given function |
Returns:
the number of arguments accepted by
f
- Type
- int
breakpoint() → {NoneType}
Evaluates to the
None value with no effect on the program. In the stepper and CSE
machine visualization, breakpoint() additionally marks a step that the breakpoint
navigation can jump to.
Returns:
the
None value
- Type
- NoneType
build_llist(fun, n) → {linked_list}
Makes a linked list with n elements by applying the unary function fun to the numbers 0 to n - 1.
Parameters:
| Name | Type | Description |
|---|---|---|
|
function | given unary function |
|
int | number of elements in the linked list |
Returns:
linked list with n elements generated by applying fun to 0 to n - 1
- Type
- linked_list
build_stream(f, n) → {stream}
Makes a stream with n
elements by applying the unary function f
to the numbers 0 to n-1.
Lazy? Yes: The result stream forces the application of f
for the next element
Parameters:
| Name | Type | Description |
|---|---|---|
|
function | given unary function |
|
int | given integer |
Returns:
resulting stream
- Type
- stream
clear_all_timeout() → {None}
Cancels every
set_timeout callback that has been scheduled but has not yet fired.
Returns:
None
- Type
- None
complex(v, i) → {complex}
Returns a complex number constructed from either zero, one or two arguments.
If no arguments are given, returns
0j. If one argument is given, it is converted to a complex numbe
r and returned.
If two arguments are given, they are interpreted as the real and imaginary parts of a complex number, respective
ly.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | string | bool | complex | If possible, value is converted
to a complex number. If omitted, it defaults to 0j. If a second argument i is given, t
he first argument cannot be a string |
|
int | float | bool | complex | The imaginary part of the complex number. If omitted, it
defaults to 0. |
Returns:
a complex number constructed from the given
value
- Type
- complex
draw_data(values)
PRIMITIVE
Visualizes the arguments in a separate drawing area in the Source
Academy using box-and-pointer diagrams.
Parameters:
| Name | Type | Description |
|---|---|---|
|
value | given values (value1, value2, ...) |
enum_llist(start, end) → {linked_list}
Makes a linked list with elements from start to end (inclusive).
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | starting element of the linked list |
|
int | ending element of the linked list |
Returns:
linked list with elements from start to end (inclusive)
- Type
- linked_list
enum_stream(start, end) → {stream}
Returns a stream that enumerates
numbers starting from start using a step size of 1, until
the integer exceeds (
>) end.
Lazy? Yes: The result stream forces the construction of
each next element
Parameters:
| Name | Type | Description |
|---|---|---|
|
integer | start - starting integer |
|
integer | end - ending integer |
Returns:
stream from start to end
- Type
- stream
error(*object) → {NoneType}
Prints the provided
*object arguments to the standard output (similar to a simplified
print) and then raises an exception. This function accepts a variable number of arguments,
converts them to their string representations using str(), outputs them (with a
newline) just like what print does, and immediately halts execution by raising an exception.
Parameters:
| Name | Type | Description |
|---|---|---|
|
any | Objects to be printed to the standard output |
Returns:
the
None value
- Type
- NoneType
eval_stream(s, n) → {linked_list}
Constructs the linked_list of the first nelements
of a given stream s
Lazy? Sort-of: eval_stream only forces the computation of
the first nelements, and leaves the rest of
the stream untouched.
Parameters:
| Name | Type | Description |
|---|---|---|
|
stream | given stream |
|
integer | given number of elements to place in result linked_list |
Returns:
result linked_list
- Type
- linked_list
filter(pred, xs) → {linked_list}
Returns a linked list that contains only those elements for which the one-argument function pred returns True.
Parameters:
| Name | Type | Description |
|---|---|---|
|
function | given one-argument function |
|
linked_list | given linked list |
Returns:
linked list that contains only those elements of xs for which pred returns True
- Type
- linked_list
for_each(fun, xs)
Applies the unary function fun to every element of the linked list xs.
Parameters:
| Name | Type | Description |
|---|---|---|
|
function | given unary function |
|
linked_list | given linked list |
head(p) → {value}
PRIMITIVE
Returns head (first component) of given pair p.
Parameters:
| Name | Type | Description |
|---|---|---|
|
pair | given pair |
Returns:
head of p
- Type
- value
imag(x) → {float}
Return the imaginary part of a complex number
x.
Parameters:
| Name | Type | Description |
|---|---|---|
|
complex | a complex number |
Returns:
the imaginary part of
x
- Type
- float
input(prompt) → {string}
If the
prompt argument is present, it is written to standard output without a trailing newline.
The function then reads a line from input, converts it to a string (stripping a trailing newline),
and returns that.
Parameters:
| Name | Type | Description |
|---|---|---|
|
string | An optional string that is written to standard output
(without a trailing newline) before input is read. |
Returns:
the input read from the user as a
string, with any trailing newline removed
- Type
- string
integers_from(start) → {stream}
Returns infinite stream if numbers starting
at given integer n using a step size of 1.
Lazy? Yes: The result stream forces the construction of
each next element
Parameters:
| Name | Type | Description |
|---|---|---|
|
integer | start - starting integer |
Returns:
infinite stream from n
- Type
- stream
is_boolean(x) → {bool}
Returns True if x is a boolean value (True or False), and False otherwise.
Parameters:
| Name | Type | Description |
|---|---|---|
|
any | given value |
Returns:
whether x is a boolean value
- Type
- bool
is_complex(x) → {bool}
Returns True if x is a complex number, and False otherwise.
Parameters:
| Name | Type | Description |
|---|---|---|
|
any | given value |
Returns:
whether x is a complex number
- Type
- bool
is_float(x) → {bool}
Returns True if x is a floating-point number, and False otherwise.
Parameters:
| Name | Type | Description |
|---|---|---|
|
any | given value |
Returns:
whether x is a floating-point number
- Type
- bool
is_function(x) → {bool}
Returns True if x is a function, and False otherwise.
Parameters:
| Name | Type | Description |
|---|---|---|
|
any | given value |
Returns:
whether x is a function
- Type
- bool
is_integer(x) → {bool}
Returns True if x is an integer, and False otherwise.
Parameters:
| Name | Type | Description |
|---|---|---|
|
any | given value |
Returns:
whether x is an integer
- Type
- bool
is_list(x) → {boolean}
PRIMITIVE
returns True if x is a list and False otherwise.
Parameters:
| Name | Type | Description |
|---|---|---|
|
value | given value |
Returns:
whether x is a list
- Type
- boolean
is_llist(xs) → {boolean}
PRIMITIVE
Returns True if xs is a linked list as defined in the textbook, and
False otherwise.
Parameters:
| Name | Type | Description |
|---|---|---|
|
value | given value |
Returns:
whether xs is a linked list
- Type
- boolean
is_none(x) → {bool}
Returns True if x is the None value, and False otherwise.
Parameters:
| Name | Type | Description |
|---|---|---|
|
any | given value |
Returns:
whether x is the None value
- Type
- bool
is_none(x) → {boolean}
PRIMITIVE
Returns True if x is the empty linked list None, and False otherwise.
Parameters:
| Name | Type | Description |
|---|---|---|
|
value | given value |
Returns:
whether x is None
- Type
- boolean
is_number(x) → {bool}
Returns True if x is a number, and False otherwise. A number is an integer,
a floating-point number or a complex number; a boolean is not a number.
Parameters:
| Name | Type | Description |
|---|---|---|
|
any | given value |
Returns:
whether x is a number
- Type
- bool
is_pair(x) → {boolean}
PRIMITIVE
Returns True if x is a pair and False otherwise.
Parameters:
| Name | Type | Description |
|---|---|---|
|
value | given value |
Returns:
whether x is a pair
- Type
- boolean
is_stream(xs) → {boolean}
Returns True if xs is a stream as defined in the textbook,
and False otherwise. Iterative process.
Recurses down the stream and checks that it ends with the empty stream None.
Laziness: No: is_stream needs to force the given stream.
Parameters:
| Name | Type | Description |
|---|---|---|
|
value | given value |
Returns:
whether xs is a stream
- Type
- boolean
is_string(x) → {bool}
Returns True if x is a string, and False otherwise.
Parameters:
| Name | Type | Description |
|---|---|---|
|
any | given value |
Returns:
whether x is a string
- Type
- bool
len(s) → {int}
Return the length of
s, where s is a list or a string.
Parameters:
| Name | Type | Description |
|---|---|---|
|
string | list | a list or a string whose length is to be computed |
Returns:
the length of
s
- Type
- int
length(xs) → {int}
Returns the length of the linked list xs.
Parameters:
| Name | Type | Description |
|---|---|---|
|
linked_list | given linked list |
Returns:
length of xs
- Type
- int
list_length(x) → {int}
PRIMITIVE
the current length of list x, which is 1 plus the
highest index that has been used so far in a list assignment on
x. Here literal list expressions are counted too: The
list [10, 20, 30] has a length of 3.
Parameters:
| Name | Type | Description |
|---|---|---|
|
list | given value |
Returns:
current length of list
- Type
- int
llist(values) → {linked_list}
PRIMITIVE
Given n values, returns a linked list of length n. The elements of
the linked list are the given values in the given order.
Parameters:
| Name | Type | Description |
|---|---|---|
|
value | given values (value1, value2, ...) |
Returns:
linked list containing all values
- Type
- linked_list
llist_ref(xs, n) → {value}
Returns the element of linked list xs at position n (0-indexed).
Parameters:
| Name | Type | Description |
|---|---|---|
|
linked_list | given linked list |
|
int | index of the element to return |
Returns:
element of xs at position n
- Type
- value
llist_to_stream(xs) → {stream}
Given linked_list xs, returns a stream of same length with
the same elements as xs in the same order.
Laziness: Yes: llist_to_stream
goes down the linked_list only when forced.
Parameters:
| Name | Type | Description |
|---|---|---|
|
linked_list | given linked_list |
Returns:
stream containing all elements of xs
- Type
- stream
llist_to_string(xs) → {string}
Returns a string that represents linked list xs using the text-based box-and-pointer notation.
Parameters:
| Name | Type | Description |
|---|---|---|
|
linked_list | given linked list |
Returns:
string that represents xs using box-and-pointer notation
- Type
- string
map(f, xs) → {linked_list}
Returns a linked list that results from linked list xs by element-wise application of unary function f.
Parameters:
| Name | Type | Description |
|---|---|---|
|
function | given unary function |
|
linked_list | given linked list |
Returns:
linked list that results from element-wise application of f to xs
- Type
- linked_list
math_acos(x) → {float}
Return the arc cosine of
x, in radians. The result is between 0 and pi.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The value whose arc cosine is to be computed. Must be in the interval
[-1, 1]. |
Returns:
the arc cosine of
x in radians
- Type
- float
math_acosh(x) → {float}
Return the inverse hyperbolic cosine of
x.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The number for which to compute the inverse hyperbolic cosine.
(Typically, x must be ≥ 1.) |
Returns:
the inverse hyperbolic cosine of
x
- Type
- float
math_asin(x) → {float}
Return the arc sine of
x, in radians. The result is between -pi/2 and pi/2.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The value whose arc sine is to be computed. Must be in the interval
[-1, 1]. |
Returns:
the arc sine of
x in radians
- Type
- float
math_asinh(x) → {float}
Return the inverse hyperbolic sine of
x.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The number for which to compute the inverse hyperbolic sine. |
Returns:
the inverse hyperbolic sine of
x
- Type
- float
math_atan(x) → {float}
Return the arc tangent of
x, in radians. The result is between -pi/2 and pi/2.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The value whose arc tangent is to be computed. |
Returns:
the arc tangent of
x in radians
- Type
- float
math_atan2(y, x) → {float}
Return
atan(y / x), in radians.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The y-coordinate of the point. |
|
int | float | The x-coordinate of the point. |
Returns:
the arc tangent of
y/x in radians
- Type
- float
math_atanh(x) → {float}
Return the inverse hyperbolic tangent of
x.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The number for which to compute the inverse hyperbolic tangent.
(Must be in the interval (-1, 1).) |
Returns:
the inverse hyperbolic tangent of
x
- Type
- float
math_cbrt(x) → {float}
Return the cube root of
x.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The numeric value for which to compute the cube root. |
Returns:
the cube root of
x
- Type
- float
math_ceil(x) → {int}
Return the ceiling of
x, the smallest integer greater than or equal to
x.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The numeric value for which to compute the ceiling. |
Returns:
the ceiling of
x
- Type
- int
math_comb(n, k) → {int}
Return the number of ways to choose
k items from n items
without repetition and without order.
Returns zero when k > n.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | Total number of items (must be a non-negative integer). |
|
int | Number of items to choose (must be a non-negative integer). |
Returns:
the binomial coefficient of
n and k
- Type
- int
math_copysign(x, y) → {float}
Return a
float with the magnitude (absolute value) of x
but the sign of y.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The value whose magnitude (absolute value) will be used. |
|
int | float | The value whose sign will be applied to x's magnitude. |
Returns:
a
float with the absolute value of x but with the sign of
y
- Type
- float
math_cos(x) → {float}
Return the cosine of
x radians.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The angle in radians for which the cosine is computed. |
Returns:
the cosine of
x
- Type
- float
math_cosh(x) → {float}
Return the hyperbolic cosine of
x.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The angle in radians for which to compute cosh(x). |
Returns:
the hyperbolic cosine of
x
- Type
- float
math_degrees(x) → {float}
Convert angle
x from radians to degrees.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The angle in radians to be converted to degrees. |
Returns:
the angle, in degrees, corresponding to the given radians
- Type
- float
math_erf(x) → {float}
Return the error function at
x.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The value at which to evaluate the error function. |
Returns:
the error function value at
x
- Type
- float
math_erfc(x) → {float}
Return the complementary error function at
x. The complementary error function is
defined as 1.0 - erf(x). It is used for large values of x where a subtraction
from one would cause a loss of significance.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The value at which to evaluate the complementary error function. |
Returns:
the complementary error function at
x
- Type
- float
math_exp(x) → {float}
Return
e raised to the power x, where e = 2.718281…
is the base of natural logarithms.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The exponent for which to compute e^x. |
Returns:
the value of
e raised to the power x with high accuracy
- Type
- float
math_exp2(x) → {float}
Return
2 raised to the power x.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The exponent for which to compute 2^x. |
Returns:
the value of
2 raised to the power x with high accuracy
- Type
- float
math_expm1(x) → {float}
Return
e raised to the power x, minus 1. Here e is
the base of natural logarithms. For small x, the subtraction in
exp(x) - 1 can result in a significant loss of precision; the expm1() function
provides a way to compute this quantity to full precision.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The exponent for which to compute e^x. |
Returns:
the value of
e raised to the power x minus 1 with high accuracy
- Type
- float
math_fabs(x) → {float}
Return the absolute value of a number as a float.
Unlike the built-in
abs(), math_fabs() always returns a float,
even when the input is an integer.
It only accepts int or float types (complex numbers are not supported).
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The number whose absolute value is computed. |
Returns:
absolute value of
x
- Type
- float
math_factorial(n) → {int}
Return
n factorial as an integer.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | A non-negative integer whose factorial is to be computed. |
Returns:
the factorial of
n
- Type
- int
math_floor(x) → {int}
Return the floor of
x, the largest integer less than or equal to
x.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The numeric value for which to compute the flooring. |
Returns:
the flooring of
x
- Type
- int
math_fma(x, y, z) → {float}
Fused multiply–add operation. Return
(x * y) + z, computed as though with infinite
precision and range followed by a single round to the float format.
This operation often provides better accuracy than the direct expression
(x * y) + z.
This function follows the specification of the
(fusedMultiplyAdd) operation described in the IEEE 754 standard.
The standard leaves one case implementation-defined, namely the result of
fma(0, inf, nan) and fma(inf, 0, nan).
In these cases, math.fma returns a math.nan, and does not raise any exception.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The first multiplicand. It is multiplied by y. |
|
int | float | The second multiplicand. It is multiplied by x. |
|
int | float | The addend. The product of x and y
is added to z using a fused multiply–add operation. |
Returns:
the float value of
(x * y) + z
- Type
- float
math_fmod(x, y) → {float}
Return the floating-point remainder of
x / y, as defined by the
platform C library function fmod(x, y). The sign of the result is the same as the
sign of x.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The dividend. It will be converted to a float
if necessary. |
|
int | float | The divisor. It will be converted to a float
if necessary. |
Returns:
the platform C library function
fmod(x, y) style remainder of
x divided by y
- Type
- float
math_gamma(x) → {float}
Return the Gamma function at
x.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The input value at which the Gamma function is computed. |
Returns:
the Gamma function at
x
- Type
- float
math_gcd(*integers) → {int}
Return the greatest common divisor of the specified
*integers arguments.
If any of the arguments is nonzero, then the returned value is the largest positive
integer that is a divisor of all arguments.
If all arguments are 0, then the returned value is 0.
gcd() without arguments returns 0.
If any of the provided integers is negative, the function treats it as its
absolute value when computing the GCD.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | A variable number of integer arguments for which to compute the greatest common divisor. |
Returns:
the greatest common divisor of the given
integers as a positive
integer
- Type
- int
math_isfinite(x) → {bool}
Return
True if x is neither an infinity nor a nan,
and False otherwise.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | A numeric value. It is converted to float if necessary. |
Returns:
the
True if x is finite; otherwise, False
- Type
- bool
math_isinf(x) → {bool}
Return
True if x is a positive or negative infinity,
and False otherwise.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | A numeric value. It is converted to float if necessary. |
Returns:
the
True if x is an infinity (positive or negative);
otherwise, False
- Type
- bool
math_isnan(x) → {bool}
Return
True if x is a nan (not a number),
and False otherwise.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | A numeric value. It is converted to float if necessary. |
Returns:
the
True if x is nan; otherwise, False
- Type
- bool
math_isqrt(n) → {int}
Return the integer square root of the non-negative
n.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | A non-negative integer for which to compute the integer square root. |
Returns:
the integer square root of
n
- Type
- int
math_lcm(*integers) → {int}
Return the least common multiple of the specified integer arguments.
If all arguments are nonzero, then the returned value is the smallest positive
integer that is a multiple of all arguments.
If any of the arguments is
0, then the returned value is 0.
lcm() without arguments returns 1.
If any of the input integers is negative, math_lcm() treats it
as its absolute value when computing the LCM, so the result is always
non-negative.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | A variable number of integer arguments for which the least common multiple is computed. |
Returns:
the least common multiple of the given integers as a positive
integer
- Type
- int
math_ldexp(x, i) → {float}
Return
x * (2**i). This is essentially the inverse of function
frexp().
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | A numeric value (the significand). It is converted to
float if necessary. |
|
int | An integer exponent. |
Returns:
the result of
x multiplied by 2 raised to the power
i
- Type
- float
math_lgamma(x) → {float}
Return the natural logarithm of the absolute value of the Gamma function at
x.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The input value for which to compute the natural logarithm of the absolute Gamma function. |
Returns:
the natural logarithm of the absolute value of the Gamma function at
x
- Type
- float
math_log(x, base(optional)) → {float}
With one argument, return the natural logarithm of
x (to base e).
With two arguments, return the logarithm of x to the given base,
calculated as log(x)/log(base).
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The numeric value for which to compute the logarithm. |
|
int | float | The base of the logarithm. If provided, the result is computed as
log(x)/log(base). If omitted, the natural logarithm (base e) is returned. |
Returns:
a float representing the logarithm of
x (either natural logarithm when
base is not provided, or logarithm with the given base otherwise)
- Type
- float
math_log10(x) → {float}
Return the base-10 logarithm of
x. This is usually more accurate than
log(x, 10).
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | A positive number. The function returns the logarithm of
x to base 10. |
Returns:
the base-10 logarithm of
x
- Type
- float
math_log1p(x) → {float}
Return the natural logarithm of
1+x (base e). The result is calculated in a way
which is accurate for x near 0.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The number to be added to 1. The function returns the natural
logarithm of (1+x), computed in a way that is accurate for values of x near 0. |
Returns:
the natural logarithm of
1+x (base e)
- Type
- float
math_log2(x) → {float}
Return the base-2 logarithm of
x. This is usually more accurate than
log(x, 2).
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | A positive number. The function returns the logarithm of
x to base 2. |
Returns:
the base-2 logarithm of
x
- Type
- float
math_nextafter(x, y, steps) → {float}
Return the floating-point value
steps steps after x towards
y. If x is equal to y, return y, unless
steps is 0.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The starting floating-point number from which the stepping begins. |
|
int | float | The target value that determines the direction. The function will
return a value toward y from x. |
|
int | The number of representable floating-point values to step from
x toward y (default is 1). |
Returns:
the floating-point number that is exactly
steps representable numbers
away from x in the direction of y
- Type
- float
math_perm(n, k) → {int}
Return the number of ways to choose
k items from n items without
repetition and with order.
Returns zero when k > n.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | Total number of items (must be a non-negative integer). |
|
int | Number of items to choose (must be a non-negative integer). |
Returns:
the permutations of
n and k
- Type
- int
math_pow(x, y) → {float}
Return
x raised to the power y. Unlike the built-in
** operator, math_pow() converts both its arguments to type float.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The base value. Both x and y are converted
to float before the operation. |
|
int | float | The exponent value. The function computes x raised to the power
y, following IEEE 754 rules for special cases. |
Returns:
the value of
x raised to the power y
- Type
- float
math_radians(x) → {float}
Convert angle
x from degrees to radians.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The angle in degrees to be converted to radians. |
Returns:
the angle, in radians, corresponding to the given degrees
- Type
- float
math_remainder(x, y) → {float}
Return the IEEE 754-style remainder of
x with respect to y. For finite
x and finite nonzero y, this is the difference x - n*y, where
n is the closest integer to the exact value of the quotient x / y.
If x / y is exactly halfway between two consecutive integers, the nearest
even integer is used for n. The remainder r = remainder(x, y)
thus always satisfies abs(r) <= 0.5 * abs(y).
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The dividend. It will be converted to a float
if necessary. |
|
int | float | The divisor. It will be converted to a float
if necessary. |
Returns:
the IEEE 754-style remainder of
x divided by y
- Type
- float
math_sin(x) → {float}
Return the sine of
x radians.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The angle in radians for which the sine is computed. |
Returns:
the sine of
x
- Type
- float
math_sinh(x) → {float}
Return the hyperbolic sine of
x.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The angle in radians for which to compute sinh(x). |
Returns:
the hyperbolic sine of
x
- Type
- float
math_sqrt(x) → {float}
Return the square root of
x.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | A non-negative number. x is converted to a float
if necessary. |
Returns:
the square root of
x
- Type
- float
math_tan(x) → {float}
Return the tangent of
x radians.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The angle in radians for which the tangent is computed. |
Returns:
the tangent of
x
- Type
- float
math_tanh(x) → {float}
Return the hyperbolic tangent of
x.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The angle in radians for which to compute tanh(x). |
Returns:
the hyperbolic tangent of
x
- Type
- float
math_trunc(x) → {int}
Return
x with the fractional part removed, leaving the integer part.
trunc() is equivalent to floor() for positive x, and equivalent
to ceil() for negative x.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The numeric value from which the fractional part is removed,
returning the integral part (i.e. x rounded toward 0). |
Returns:
the integer part of
x
- Type
- int
math_ulp(x) → {float}
Return the value of the least significant bit of the
float x.
If x is a NaN (not a number), return x.
If x is negative, return ulp(-x).
If x is a positive infinity, return x.
If x is equal to 0, return the smallest positive denormalized
representable float (smaller than the minimum positive normalized float,
sys.float_info.min, approximately 1.7976931348623157e+308).
If x is equal to the largest positive representable float, return the value
of the least significant bit of x, such that the first float smaller than
x is x - ulp(x).
Otherwise (when x is a positive finite number), return the value of the least significant
bit of x, such that the first float bigger than x is
x + ulp(x).
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The numeric value (typically a float) for which to compute
the ULP (Unit in the Last Place). The function returns the value of the least significant
bit of x, handling special cases (NaN, infinities, 0, etc.)
as specified by IEEE 754. |
Returns:
the spacing between
x and the next representable float in the
direction defined by x's sign
- Type
- float
max(arg1, arg2, *args) → {int|float|string}
Returns the largest of the provided values. If multiple items are equal to the maximum,
the first encountered is returned. All values should be mutually comparable.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | string | The first item to compare. |
|
int | float | string | The second item to compare. |
|
int | float | string | Additional items to compare. |
Returns:
the largest of the provided values
- Type
- int | float | string
member(v, xs) → {linked_list}
Returns first postfix sub-linked list whose head is identical to v (using ==). Returns None if the element does not occur in the linked list.
Parameters:
| Name | Type | Description |
|---|---|---|
|
value | given value |
|
linked_list | given linked list |
Returns:
first postfix sub-linked list of xs whose head is identical to v, or None if no such sub-linked list exists
- Type
- linked_list
min(arg1, arg2, *args) → {int|float|string}
Returns the smallest of the provided values. If multiple items are equal to the minimum,
the first encountered is returned. Arguments must be all comparable numbers (int or float) or all strings.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | string | The first item to compare. |
|
int | float | string | The second item to compare. |
|
int | float | string | Additional items to compare. |
Returns:
the smallest of the provided values
- Type
- int | float | string
pair(x, y) → {pair}
PRIMITIVE
Makes a pair whose head (first component) is x and whose tail (second component) is y.
Parameters:
| Name | Type | Description |
|---|---|---|
|
value | given head |
|
value | given tail |
Returns:
pair with x as head and y as tail
- Type
- pair
print(*object) → {NoneType}
A simplified version of the Python built-in
print function.
This function takes any number of parameters *object, converts them to their
string representations using str(), and writes them to the standard
output (sys.stdout), followed by a newline character. See the official Python
documentation for print.
Parameters:
| Name | Type | Description |
|---|---|---|
|
any | object(s) to be printed to the standard output |
Returns:
the
None value
- Type
- NoneType
random_random() → {float}
Return the next random floating-point number in the range
0.0 ≤ X < 1.0.
Returns:
the next random floating-point number in the range
0.0 ≤ X < 1.0
- Type
- float
real(x) → {float}
Return the real part of a complex number
x.
Parameters:
| Name | Type | Description |
|---|---|---|
|
complex | a complex number |
Returns:
the real part of
x
- Type
- float
reduce(f, initial, xs) → {value}
Applies binary function f to the elements of xs from right-to-left order.
Parameters:
| Name | Type | Description |
|---|---|---|
|
function | given binary function |
|
value | initial value for the accumulation |
|
linked_list | given linked list |
Returns:
result of applying f to the elements of xs from right-to-left order, starting with initial
- Type
- value
remove(v, xs) → {linked_list}
Returns a linked list that results from xs by removing the first item from xs that is identical (==) to v.
Parameters:
| Name | Type | Description |
|---|---|---|
|
value | given value |
|
linked_list | given linked list |
Returns:
linked list that results from removing the first item from xs that is identical to v
- Type
- linked_list
remove_all(v, xs) → {linked_list}
Returns a linked list that results from xs by removing all items from xs that are identical (==) to v.
Parameters:
| Name | Type | Description |
|---|---|---|
|
value | given value |
|
linked_list | given linked list |
Returns:
linked list that results from removing all items from xs that are identical to v
- Type
- linked_list
repr(object) → {string}
Return a
string representation of object that is unambiguous and suitable for debugging.
For many values, this string can be used to reconstruct object
Parameters:
| Name | Type | Description |
|---|---|---|
|
any | The object to be converted to a string. |
Returns:
the unambiguous
string representation of object
- Type
- string
reverse(xs) → {linked_list}
Returns linked list xs in reverse order.
Parameters:
| Name | Type | Description |
|---|---|---|
|
linked_list | given linked list |
Returns:
linked list xs in reverse order
- Type
- linked_list
round(number, ndigits) → {float}
Return
number rounded to ndigits precision after the decimal point. If
ndigits is omitted or is None, it returns the nearest integer
to its input.
Parameters:
| Name | Type | Description |
|---|---|---|
|
int | float | The value to be rounded. |
|
int | The number of digits to round to after the decimal point. If omitted
or None, the function rounds to the nearest integer. |
Returns:
the number rounded to
ndigits precision
- Type
- float
set_head(p, x) → {None}
changes the pair p such that its head is x.
Parameters:
| Name | Type | Description |
|---|---|---|
|
pair | given value |
|
value | given value |
Returns:
None
- Type
- None
set_tail(p, x) → {None}
changes the pair p such that its tail is x.
Parameters:
| Name | Type | Description |
|---|---|---|
|
pair | given value |
|
value | given value |
Returns:
None
- Type
- None
set_timeout(f, t) → {None}
Schedules
f to be called with no arguments after t milliseconds have
elapsed, without blocking the calling code. f is invoked once, the next time the
scheduled delay elapses.
Parameters:
| Name | Type | Description |
|---|---|---|
|
function | the function to call once t milliseconds have elapsed |
|
int | float | the delay, in milliseconds, before f is called |
Returns:
None
- Type
- None
str(object) → {string}
Return a
string version of object. If object is not provided, returns
the empty string.
Parameters:
| Name | Type | Description |
|---|---|---|
|
any | The object to be converted to a string.
If not provided, an empty string is returned. |
Returns:
the informal
string representation of object
- Type
- string
stream(…values) → {stream}
Given n values, returns a stream of length n.
The elements of the stream are the given values in the given order.
Lazy? No: A complete linked linked_list is generated,
and then a stream using llist_to_stream is generated from it.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
|
value |
<repeatable> |
given values (value1, value2, ...) |
Returns:
stream containing all values
- Type
- stream
stream_append(xs, ys) → {stream}
Returns a stream that results from
appending the stream ys to the streamxs.
In the result, None at the end of the first argument stream
is replaced by the second argument, regardless what the second
argument consists of.
Lazy? Yes: the result stream forces the actual append operation
Parameters:
| Name | Type | Description |
|---|---|---|
|
stream | given first stream |
|
stream | given second stream |
Returns:
result of appending xs and ys
- Type
- stream
stream_filter(pred, s) → {stream}
Returns a stream that contains
only those elements of given stream xs
for which the one-argument function
pred
returns True.
Lazy? Yes: The result stream forces the construction of
each next element. Of course, the construction
of the next element needs to go down the stream
until an element is found for which pred holds.
Parameters:
| Name | Type | Description |
|---|---|---|
|
function | given pred - unary function returning boolean value |
|
stream | given stream |
Returns:
stream with those elements of xs for which pred holds.
- Type
- stream
stream_for_each(f, xs) → {boolean}
Applies unary function f to every
element of the stream xs.
Iterative process.
f is applied element-by-element:
stream_for_each(f, stream(1, 2)) results in the calls
f(1) and f(2).
Lazy? No: stream_for_each
forces the exploration of the entire stream
Parameters:
| Name | Type | Description |
|---|---|---|
|
function | given unary function |
|
stream | given stream |
Returns:
True
- Type
- boolean
stream_length(xs) → {integer}
Returns the length of the stream xs.
Iterative process.
Lazy? No: The function needs to explore the whole stream
Parameters:
| Name | Type | Description |
|---|---|---|
|
stream | given stream |
Returns:
length of xs
- Type
- integer
stream_map(f, xs) → {stream}
Returns a stream that results from stream
xsby element-wise application
of unary function f.
f is applied element-by-element:
stream_map(f, stream(1,2)) results in
the same as stream(f(1),f(2)).
Lazy? Yes: The argument stream is only explored as forced by
the result stream.
Parameters:
| Name | Type | Description |
|---|---|---|
|
function | given unary function |
|
stream | given stream |
Returns:
result of mapping
- Type
- stream
stream_member(x, s) → {stream}
Returns first postfix substream
whose head is identical to
v (using ==); returns None if the
element does not occur in the stream.
Iterative process.
Lazy? Sort-of:
stream_member
forces the stream only until the element
is found.
Parameters:
| Name | Type | Description |
|---|---|---|
|
value | given value |
|
stream | given stream |
Returns:
postfix substream that starts with x
- Type
- stream
stream_ref(s, n) → {value}
Returns the element
of stream xs at position n,
where the first element has index 0.
Iterative process.
Lazy? Sort-of: stream_ref only forces the computation of
the first n elements, and leaves the rest of
the stream untouched.
Parameters:
| Name | Type | Description |
|---|---|---|
|
stream | given stream |
|
integer | given position |
Returns:
item in xs at position n
- Type
- value
stream_remove(v, xs) → {stream}
Returns a stream that results from
xs by removing the first item from xs that
is identical (
===) to v.
Returns the original
stream if there is no occurrence.
Lazy? Yes: the result stream forces the construction of each next element
Parameters:
| Name | Type | Description |
|---|---|---|
|
value | given value |
|
stream | given stream |
Returns:
xs with first occurrence of v removed
- Type
- stream
stream_remove_all(v, xs) → {stream}
Returns a stream that results from
xs by removing all items from xs that
are identical (
==) to v.
Returns the original
stream if there is no occurrence.
Recursive process.
Lazy? Yes: the result stream forces the construction of each next
element
Parameters:
| Name | Type | Description |
|---|---|---|
|
value | given value |
|
stream | given stream |
Returns:
xs with all occurrences of v removed
- Type
- stream
stream_reverse(xs) → {stream}
Returns stream xs in reverse
order. Iterative process.
The process is iterative, but consumes space Omega(n)
because of the result stream.
Lazy? No: stream_reverse
forces the exploration of the entire stream
Parameters:
| Name | Type | Description |
|---|---|---|
|
stream | given stream |
Returns:
xs in reverse
- Type
- stream
stream_tail(xs) → {stream}
assumes that the tail (second component) of the
pair {x} expects 2 arguments, and returns the result of
applying that function. Throws an error if the argument
is not a pair, or if the tail is not a function.
Laziness: Yes: {stream_tail} only forces the direct tail
stream, but not the rest of the stream, i.e. not the tail
of the tail, etc.
Parameters:
| Name | Type | Description |
|---|---|---|
|
stream | given value |
Returns:
result stream (if stream discipline is used)
- Type
- stream
stream_to_llist(xs) → {linked_list}
Given stream xs, returns a linked_list of same length with
the same elements as xs in the same order.
Laziness: No: stream_to_llist needs to force the whole
stream.
Parameters:
| Name | Type | Description |
|---|---|---|
|
stream | given stream |
Returns:
containing all elements of xs
- Type
- linked_list
tail(p) → {value}
PRIMITIVE
Returns tail (second component) of given pair p.
Parameters:
| Name | Type | Description |
|---|---|---|
|
pair | given pair |
Returns:
tail of p
- Type
- value
time_time() → {float}
Return the number of milliseconds elapsed since
January 1, 1970 00:00:00 UTC.
Returns:
current time in milliseconds
- Type
- float