Predeclared in Source §2

Constants

(constant) Infinity :number

The name Infinity refers to the special number value Infinity. See ECMAScript Specification, Section 4.3.23
• number

(constant) math_E :number

The Number value for e, Euler's number, which is approximately 2.718281828459045.
• number

(constant) math_LN2 :number

The Number value for the natural logarithm of 2, which is approximately 0.6931471805599453.
• number

(constant) math_LN10 :number

The Number value for the natural logarithm of 10, which is approximately 2.302585092994046.
• number

(constant) math_LOG2E :number

The Number value for the base-2 logarithm of eℝ, the base of the natural logarithms; this value is approximately 1.4426950408889634.

NOTE: The value of math_LOG2E is approximately the reciprocal of the value of math_LN2.

• number

(constant) math_LOG10E :number

The Number value for the base-10 logarithm of e, the base of the natural logarithms; this value is approximately 0.4342944819032518.

NOTE: The value of math_LOG10E is approximately the reciprocal of the value of math_LN10.

• number

(constant) math_PI :number

The Number value for π, the ratio of the circumference of a circle to its diameter, which is approximately 3.1415926535897932.
• number

(constant) math_SQRT1_2 :number

The Number value for the square root of 0.5, which is approximately 0.7071067811865476.

NOTE: The value of math_SQRT1_2 is approximately the reciprocal of the value of math_SQRT2.

• number

(constant) math_SQRT2 :number

The Number value for the square root of 2, which is approximately 1.4142135623730951.
• number

(constant) NaN :number

The name NaN refers to the special number value NaN ("not a number"). Note that NaN is a number, as specified by is_number. See ECMAScript Specification, Section 4.3.24
• number

(constant) undefined :undefined

The name undefined refers to the special value undefined. See also textbook explanation in section 4.1.1.

Functions

accumulate(f, initial, xs) → {value}

Applies binary function f to the elements of xs from right-to-left order, first applying f to the last element and the value initial, resulting in r1, then to the second-last element and r1, resulting in r2, etc, and finally to the first element and rn-1, where n is the length of the list. Thus, accumulate(f,zero,list(1,2,3)) results in f(1, f(2, f(3, zero))). Iterative process; time: O(n), space: O(n), where n is the length of xs assuming f takes constant time.
Parameters:
Name Type Description
f function binary function
initial value initial value
xs list given list
Returns:
result of accumulating xs using f starting with initial
Type
value

append(xs, ys) → {list}

Returns a list that results from appending the list ys to the list xs. Iterative process; time: O(n), space: O(n), where n is the length of xs. In the result, null at the end of the first argument list is replaced by the second argument, regardless what the second argument consists of.
Parameters:
Name Type Description
xs list given first list
ys list given second list
Returns:
result of appending xs and ys
Type
list

arity(f) → {number}

Returns the number of parameters the given function f expects, excluding the rest parameter.
Parameters:
Name Type Description
f function given function
Returns:
number of parameters f expects
Type
number

build_list(f, n) → {list}

Makes a list with n elements by applying the unary function f to the numbers 0 to n - 1, assumed to be a nonnegative integer. Iterative process; time: O(n), space: O(n).
Parameters:
Name Type Description
f function unary function
n number given nonnegative integer
resulting list
Type
list

char_at(s, i) → {string}

Takes a string s as first argument and a nonnegative integer i as second argument. If i is less than the length of s, this function returns a one-character string that contains the character of s at position i, counting from 0. If i is larger than or equal to the length of s, this function returns undefined.
Parameters:
Name Type Description
s string given string
i number index
Returns:
one-character or undefined
Type
string

display(v, s) → {value}

Optional second argument. If present, displays the given string s, followed by a space character, followed by the value v in the console. If second argument not present, just displays the value v in the console. The notation used for the display of values is consistent with JSON, but also displays undefined, NaN, Infinity, and function objects.
Parameters:
Name Type Description
v value to be displayed
s string to be displayed, preceding v, optional argument
Returns:
v, the first argument value
Type
value

display_list(xs, s) → {value}

Optional second argument. Similar to display, but formats well-formed lists nicely if detected.
Parameters:
Name Type Description
xs value list structure to be displayed
s string to be displayed, preceding xs
Returns:
xs, the first argument value
Type
value

draw_data() → {value}

visualizes x in a separate drawing area in the Source Academy using a box-and-pointer diagram; time, space: O(n), where n is the total number of data structures such as pairs in all the separate structures provided in x.
Parameters:
Name Type Description
value1,value2,...,value_n value given values
given x
Type
value

enum_list(start, end) → {list}

Returns a list that enumerates numbers starting from start using a step size of 1, until the number exceeds (>) end. Iterative process; time: O(n), space: O(n), where n is end - start.
Parameters:
Name Type Description
start number starting number
end number ending number
Returns:
list from start to end
Type
list

equal(x, y) → {boolean}

Returns true if both have the same structure with respect to pair, and identical values at corresponding leave positions (places that are not themselves pairs), and false otherwise. For the "identical", the values need to have the same type, otherwise the result is false. If corresponding leaves are boolean values, these values need to be the same. If both are undefined or both are null, the result is true. Otherwise they are compared with === (using the definition of === in the respective Source language in use). Time, space: O(n), where n is the number of pairs in x.
Parameters:
Name Type Description
x value given value
y value given value
Returns:
whether x is structurally equal to y
Type
boolean

error(v, s)

Optional second argument. If present, displays the given string s, followed by a space character, followed by the value v in the console with error flag. If second argument not present, just displays the value v in the console with error flag. The evaluation of any call of error aborts the running program immediately. The notation used for the display of values is consistent with JSON, but also displays undefined, NaN, Infinity, and function objects.
Parameters:
Name Type Description
v value to be displayed
s string to be displayed, preceding v

filter(pred, xs) → {list}

Returns a list that contains only those elements for which the one-argument function pred returns true. Iterative process; time: O(n), space: O(n), where n is the length of xs.
Parameters:
Name Type Description
pred function unary function returning boolean value
xs list given list
Returns:
list with those elements of xs for which pred holds.
Type
list

for_each(f, xs) → {boolean}

Applies unary function f to every element of the list xs. Iterative process; time: O(n), space: O(1), Where n is the length of xs. f is applied element-by-element: for_each(fun, list(1, 2)) results in the calls fun(1) and fun(2).
Parameters:
Name Type Description
f function unary
xs list given list
true
Type
boolean

get_time() → {number}

Returns number of milliseconds elapsed since January 1, 1970 00:00:00 UTC. See also textbook example.
Returns:
current time in milliseconds
Type
number
returns head (first component) of given pair p
Parameters:
Name Type Description
p pair given pair
Type
value

is_boolean(v) → {boolean}

checks whether a given value is a boolean
Parameters:
Name Type Description
v value to be checked
Returns:
indicating whether the value is a boolean
Type
boolean

is_function(v) → {boolean}

checks whether a given value is a function
Parameters:
Name Type Description
v value to be checked
Returns:
indicating whether the value is a function
Type
boolean

is_list(xs) → {xs}

Returns true if xs is a list as defined in the textbook, and false otherwise. Iterative process; time: O(n), space: O(1), where n is the length of the chain of tail operations that can be applied to xs. recurses down the list and checks that it ends with the empty list null
Parameters:
Name Type Description
xs value given candidate
Returns:
whether is a list
Type
xs

is_null(x) → {boolean}

returns true if x is the empty list null, and false otherwise.
Parameters:
Name Type Description
x value given value
Returns:
whether x is null
Type
boolean

is_number(v) → {boolean}

checks whether a given value is a number. See also textbook example.
Parameters:
Name Type Description
v value to be checked
Returns:
indicating whether the value is a number
Type
boolean

is_pair(x) → {boolean}

returns true if x is a pair and false otherwise.
Parameters:
Name Type Description
x value given value
Returns:
whether x is a pair
Type
boolean

is_string(v) → {boolean}

checks whether a given value is a string. See also textbook example.
Parameters:
Name Type Description
v value to be checked
Returns:
indicating whether the value is a string
Type
boolean

is_undefined(v) → {boolean}

checks whether a given value is the special value undefined
Parameters:
Name Type Description
v value to be checked
Returns:
indicating whether the value is undefined
Type
boolean

length(xs) → {number}

Returns the length of the list xs. Iterative process; time: O(n), space: O(1), where n is the length of xs.
Parameters:
Name Type Description
xs list given list
length of xs
Type
number

list() → {list}

Given n values, returns a list of length n. The elements of the list are the given values in the given order.
Parameters:
Name Type Description
value1,value2,...,value_n value given values
Returns:
list containing all values
Type
list

list_ref(xs, n) → {value}

Returns the element of list xs at position n, where the first element has index 0. Iterative process; time: O(n), space: O(1), where n is the length of xs.
Parameters:
Name Type Description
xs list given list
n number given position
Returns:
item in xs at position n
Type
value

list_to_string(xs) → {string}

Returns a string that represents list xs using the text-based box-and-pointer notation [...].
Parameters:
Name Type Description
xs list given list
Returns:
xs converted to string
Type
string

map(f, xs) → {list}

Returns a list that results from list xs by element-wise application of unary function f. Iterative process; time: O(n), space: O(n), where n is the length of xs. f is applied element-by-element: map(f, list(1, 2)) results in list(f(1), f(2)).
Parameters:
Name Type Description
f function unary
xs list given list
Returns:
result of mapping
Type
list

math_abs(x) → {number}

computes the absolute value of x; the result has the same magnitude as x but has positive sign.
Parameters:
Name Type Description
x number given number
Returns:
absolute value of x
Type
number

math_acos(x) → {number}

computes the arc cosine of x. The result is expressed in radians and ranges from +0 to +π.
Parameters:
Name Type Description
x number given number
arc cosine of x
Type
number

math_acosh(x) → {number}

computes the inverse hyperbolic cosine of x.
Parameters:
Name Type Description
x number given number
Returns:
inverse hyperbolic cosine of x.
Type
number

math_asin(x) → {number}

computes the arc sine of x. The result is expressed in radians and ranges from -π / 2 to +π / 2.
Parameters:
Name Type Description
x number given number
arc sine of x.
Type
number

math_asinh(x) → {number}

computes the inverse hyperbolic sine of x. The result is expressed in radians and ranges from -π / 2 to +π / 2.
Parameters:
Name Type Description
x number given number
Returns:
inverse hyperbolic sine of x
Type
number

math_atan(x) → {number}

computes the arc tangent of x. The result is expressed in radians and ranges from -π / 2 to +π / 2.
Parameters:
Name Type Description
x number given number
arc tangent of x
Type
number

math_atan2(y, x) → {number}

computes the arc tangent of the quotient y / x of the arguments y and x, where the signs of y and x are used to determine the quadrant of the result. Note that it is intentional and traditional for the two-argument arc tangent function that the argument named y be first and the argument named x be second. The result is expressed in radians and ranges from -π to +π.
Parameters:
Name Type Description
y number given first number
x number given second number
Returns:
arc tangent of y / x.
Type
number

math_atanh(x) → {number}

computes the inverse hyperbolic tangent of x.
Parameters:
Name Type Description
x number given number
Returns:
inverse hyperbolic tangent of x.
Type
number

math_cbrt(x) → {number}

computes the cube root of x.
Parameters:
Name Type Description
x number given number
cube root of x.
Type
number

math_ceil(x) → {number}

computes the smallest (closest to -∞) Number value that is not less than x and is an integer. If x is already an integer, the result is x. The value of math_ceil(x) is the same as the value of -math_floor(-x).
Parameters:
Name Type Description
x number given number
Returns:
"ceiling" of the number
Type
number

math_clz32(n) → {number}

When math_clz32 is called with one argument x, the following steps are taken: Let n be ToUint32(x). Let p be the number of leading zero bits in the 32-bit binary representation of n. Return p.

NOTE:
If n is 0, p will be 32. If the most significant bit of the 32-bit binary encoding of n is 1, p will be 0.

Parameters:
Name Type Description
n number given number
Type
number

math_cos(x) → {number}

Computes the cosine of x. The argument is expressed in radians.
Parameters:
Name Type Description
x number given number
- cosine of x
Type
number

math_cosh(x) → {number}

computes the hyperbolic cosine of x.

NOTE: The value of cosh(x) is the same as (exp(x) + exp(-x)) / 2.

Parameters:
Name Type Description
x number given number
Returns:
hyperbolic cosine of x
Type
number

math_exp(x) → {number}

computes the exponential function of x (e raised to the power of x, where e is the base of the natural logarithms).
Parameters:
Name Type Description
x number given number
Returns:
e to the power of x
Type
number

math_expm1(x) → {number}

computes subtracting 1 from the exponential function of x (e raised to the power of x, where e is the base of the natural logarithms). The result is computed in a way that is accurate even when the value of x is close to 0.
Parameters:
Name Type Description
x number given number
Returns:
-1 plus e to the power of x
Type
number

math_floor(x) → {number}

computes the greatest (closest to +∞) Number value that is not greater than x and is an integer.
If x is already an integer, the result is x.

NOTE: The value of math_floor(x) is the same as the value of -math_ceil(-x).

Parameters:
Name Type Description
x number given number
floor of x
Type
number

math_fround(x) → {number}

When math_fround is called with argument x, the following steps are taken:
1. If x is NaN, return NaN.
2. If x is one of +0, -0, +∞, -∞, return x.
3. Let x32 be the result of converting x to a value in IEEE 754-2008 binary32 format using roundTiesToEven mode.
4. Let x64 be the result of converting x32 to a value in IEEE 754-2008 binary64 format.
5. Return the ECMAScript Number value corresponding to x64.
Parameters:
Name Type Description
x number given number
fround of x
Type
number

math_hypot() → {number}

computes the square root of the sum of squares of its arguments.
If no arguments are passed, the result is +0.
Parameters:
Name Type Description
value1,value2,... number given numbers
Returns:
square root of sum of squares of arguments
Type
number

math_imul(x, x) → {number}

When math_imul is called with arguments x and y, the following steps are taken:
1. Let a be ToUint32(x).
2. Let b be ToUint32(y).
3. Let product be (a × b) modulo 232.
4. If product ≥ 231, return product - 232; otherwise return product.
Parameters:
Name Type Description
x number given first number
x number given second number
- x imul y
Type
number

math_log(x) → {number}

Computes the natural logarithm of x.
Parameters:
Name Type Description
x number given number
Returns:
- natural logarithm of x
Type
number

math_log1p(x) → {number}

computes the natural logarithm of 1 + x. The result is computed in a way that is accurate even when the value of x is close to zero.
Parameters:
Name Type Description
x number given number
math_log(1 + x)
Type
number

math_log2(x) → {number}

computes the base 2 logarithm of x.
Parameters:
Name Type Description
x number given number
Returns:
base 2 logarithm of x
Type
number

math_log10(x) → {number}

computes the base 10 logarithm of x.
Parameters:
Name Type Description
x number given number
Returns:
base 10 logarithm of x
Type
number

math_max() → {number}

Given zero or more numbers, returns the largest of them.
If no arguments are given, the result is -∞.
If any value is NaN, the result is NaN. The comparison of values to determine the largest value is done using the Abstract Relational Comparison algorithm except that +0 is considered to be larger than -0.
Parameters:
Name Type Description
value1,value2,... number given numbers
largest of them
Type
number

math_min() → {number}

Given zero or more arguments, returns the smallest of them.
If no arguments are given, the result is +∞.
If any value is NaN, the result is NaN. The comparison of values to determine the smallest value is done using the Abstract Relational Comparison algorithm except that +0 is considered to be larger than -0.
Parameters:
Name Type Description
value1,value2,... number given numbers
smallest of them
Type
number

math_pow(base, exponent) → {number}

Computes the result of raising base to the power of exponent.
Parameters:
Name Type Description
base number the given base
exponent number the given exponent
Returns:
base to the power of exponent
Type
number

math_random() → {number}

Returns a number value with positive sign, greater than or equal to 0 but less than 1, chosen randomly or pseudo randomly with approximately uniform distribution over that range, using an implementation-dependent algorithm or strategy. This function takes no arguments. Each math_random function created for distinct realms must produce a distinct sequence of values from successive calls.
Returns:
random number greater than or equal to 0 but less than 1
Type
number

math_round(x) → {number}

Returns the number value that is closest to x and is an integer.
If two integers are equally close to x, then the result is the Number value that is closer to +∞. If x is already an integer, the result is x. NOTE 1: math_round(3.5) returns 4, but math_round(-3.5) returns -3.
Parameters:
Name Type Description
x number the given number
Returns:
closest integer to x
Type
number

math_sign(x) → {number}

Computes the sign of x, indicating whether x is positive, negative, or zero.
Parameters:
Name Type Description
x number the given number
Returns:
the sign (-1, 0 or +1)
Type
number

math_sin(x) → {number}

Computes the sine of x. The argument is expressed in radians.
Parameters:
Name Type Description
x number the given number
the sine of x
Type
number

math_sinh(x) → {number}

Computes the hyperbolic sine of x.

NOTE: The value of sinh(x) is the same as (exp(x) - exp(-x)) / 2.

Parameters:
Name Type Description
x number the given number
Returns:
the hyperbolic sine of x
Type
number

math_sqrt(x) → {number}

Computes the square root of x.
Parameters:
Name Type Description
x number the given number
Returns:
the square root of x
Type
number

math_tan(x) → {number}

Computes the tangent of x. The argument is expressed in radians.
Parameters:
Name Type Description
x number the given number
the tangent of x
Type
number

math_tanh(x) → {number}

Computes the hyperbolic tangent of x.

NOTE: The value of math_tanh(x) is the same as (exp(x) - exp(-x))/(exp(x) + exp(-x)).

Parameters:
Name Type Description
x number the given number
Returns:
the hyperbolic tangent of x
Type
number

math_trunc(x) → {number}

Computes the integral part of the number x, removing any fractional digits.
Parameters:
Name Type Description
x number the given number
Returns:
the integral part of x
Type
number

member(v, xs) → {list}

Returns first postfix sublist whose head is identical to v (using ===); returns null if the element does not occur in the list. Iterative process; time: O(n), space: O(1), where n is the length of xs.
Parameters:
Name Type Description
v value given value
xs list given list
Returns:
postfix sublist that starts with v
Type
list

pair(x, y) → {pair}

makes a pair whose head (first component) is x and whose tail (second component) is y.
Parameters:
Name Type Description
y value given tail
Returns:
pair with x as head and y as tail.
Type
pair

parse_int(s, i) → {number}

Interprets a given string s as an integer, using the positive integer i as radix, and returns the respective value.
Examples: parse_int("909", 10) returns the number 909, and parse_int("-1111", 2) returns the number -15.
See ECMAScript Specification, Section 18.2.5 for details.
Parameters:
Name Type Description
s string string to be converted
Returns:
result of conversion
Type
number

prompt(s) → {string}

Pops up a window that displays the string s, provides an input line for the user to enter a text, a Cancel button and an OK button. The call of prompt suspends execution of the program until one of the two buttons is pressed. If the OK button is pressed, prompt returns the entered text as a string. If the Cancel button is pressed, prompt returns a non-string value.
Parameters:
Name Type Description
s string to be displayed in popup
entered text
Type
string

remove(v, xs) → {list}

Returns a list that results from xs by removing the first item from xs that is identical (===) to v. Returns the original list if there is no occurrence. Iterative process; time: O(n), space: O(n), where n is the length of xs.
Parameters:
Name Type Description
v value given value
xs list given list
Returns:
xs with first occurrence of v removed
Type
list

remove_all(v, xs) → {list}

Returns a list that results from xs by removing all items from xs that are identical (===) to v. Returns the original list if there is no occurrence. Iterative process; time: O(n), space: O(n), where n is the length of xs.
Parameters:
Name Type Description
v value given value
xs list given list
Returns:
xs with all occurrences of v removed
Type
list

reverse(xs) → {list}

Returns list xs in reverse order. Iterative process; time: O(n), space: O(n), where n is the length of xs. The process is iterative, but consumes space O(n) because of the result list.
Parameters:
Name Type Description
xs list given list
xs in reverse
Type
list

stringify(v) → {string}

returns a string that represents the value v, using a notation that is is consistent with JSON, but also displays undefined, NaN, Infinity, and function objects. See also textbook example.
Parameters:
Name Type Description
v value the argument value
Returns:
string representation of v
Type
string

tail(p) → {value}

returns tail (second component of given pair p
Parameters:
Name Type Description
p pair given pair
tail of p
Type
value