Predeclared in Source §4

Below you find all constants and functions used in the textbook Structure and Interpretation of Computer Programs, JavaScript Adaptation (SICP JS). These constants and functions are predeclared in the language Source §4, a JavaScript sublanguage implemented in the Source Academy.

Can I use these constants and functions without the Source Academy ?

Yes, these constants and functions are provided by the NPM package sicp. You can use this package to run the JavaScript programs of SICP JS in any JavaScript system that is based on Node.js. Follow the link for installation instructions.

Constants

(constant) Infinity :number

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

(constant) math_E :number

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

(constant) math_LN2 :number

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

(constant) math_LN10 :number

The Number value for the natural logarithm of 10, which is approximately 2.302585092994046.
Type:
  • 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.

Type:
  • 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.

Type:
  • 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.
Type:
  • 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.

Type:
  • number

(constant) math_SQRT2 :number

The Number value for the square root of 2, which is approximately 1.4142135623730951.
Type:
  • 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
Type:
  • number

(constant) undefined :undefined

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

Functions

__access_export__(exports, lookup_name) → {value}

Searches for the specified name in the data structure of exported names. The data structure is a pair where the head element is the default export and the tail element is a list of pairs where each pair is a mapping from the exported name to the value being exported. If the lookup name is "default", the default export is returned instead of a named export. If the name does not exist, undefined is returned.
Parameters:
Name Type Description
exports pair The data structure of exported values
lookup_name string Name to import
Returns:
The value corresponding to the imported name
Type
value

__access_named_export__(named_exports, lookup_name) → {value}

Searches for the specified name in the data structure of exported names. The data structure is a list of pairs where each pair is a mapping from the exported name to the value being exported. If the name does not exist, undefined is returned.
Parameters:
Name Type Description
named_exports list The data structure of exported names
lookup_name string Name to import
Returns:
The value corresponding to the imported name
Type
value

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: Theta(n) (apart from f), space: Theta(n) (apart from f), where n is the length of xs.
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: Theta(n), space: Theta(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

apply_in_underlying_javascript(f, xs) → {whatever}

calls the function f with arguments given in list xs. For example:
function times(x, y) {
return x * y;
}
apply_in_underlying_javascript(times, list(2, 3)); // returns 6
Parameters:
Name Type Description
f function function to be applied
xs list arguments given in list
Returns:
whatever f returns
Type
whatever

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

array_length(x) → {number}

**primitive**; returns the current length of array x, which is 1 plus the highest index that has been used so far in an array assignment on x. Here literal array expressions are counted too: The array [10, 20, 30] has a length of 3. Time: Θ(1), space: Θ(1)
Parameters:
Name Type Description
x array given array
Returns:
current length of array
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: Theta(n) (apart from f), space: Theta(n) (apart from f).
Parameters:
Name Type Description
f function unary function
n number given nonnegative integer
Returns:
resulting list
Type
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, assumed to be a nonnegative integer. Lazy? Yes: The result stream forces the application of f for the next element
Parameters:
Name Type Description
f function unary function
n number given nonnegative integer
Returns:
resulting stream
Type
stream

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; time, space: Theta(n), where n is the total number of data structures such as pairs in x.
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 the arguments in a separate drawing area in the Source Academy using box-and-pointer diagrams; time, space: Theta(n), where n is the total number of data structures such as pairs in the arguments.
Parameters:
Name Type Description
value1,value2,...,value_n value given values
Returns:
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: Theta(n), space: Theta(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

enum_stream(start, end) → {stream}

Returns a stream that enumerates numbers starting from start using a step size of 1, until the number exceeds (>) end. Lazy? Yes: The result stream forces the construction of each next element
Parameters:
Name Type Description
start number starting number
end number ending number
Returns:
stream from start to end
Type
stream

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: Theta(n), where n is the total number of data structures such as pairs in x and y.
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

eval_stream(s, n) → {list}

Constructs the list of the first n elements of a given stream s Lazy? Sort-of: eval_stream only forces the computation of the first n elements, and leaves the rest of the stream untouched.
Parameters:
Name Type Description
s stream given stream
n number nonnegative number of elements to place in result list
Returns:
result list
Type
list

filter(pred, xs) → {list}

Returns a list that contains only those elements for which the one-argument function pred returns true. Iterative process; time: Theta(n) (apart from pred), space: Theta(n) (apart from pred), 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: Theta(n) (apart from f), space: Theta(1) (apart from f), 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
Returns:
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
**primitive**; returns head (first component) of given pair p; time: Theta(1)Theta(1).
Parameters:
Name Type Description
p pair given pair
Returns:
head of p
Type
value

integers_from(start) → {stream}

Returns infinite stream if integers starting at given number n using a step size of 1. Lazy? Yes: The result stream forces the construction of each next element
Parameters:
Name Type Description
start number starting number
Returns:
infinite stream from n
Type
stream

is_array(x) → {boolean}

**primitive**; returns true if x is an array, and false if it is not. Time: Θ(1), space: Θ(1)
Parameters:
Name Type Description
x value given value
Returns:
whether x is an array
Type
boolean

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}

**primitive**; returns true if xs is a list as defined in the textbook, and false otherwise. Iterative process; time: Theta(n), space: Theta(1), where n is the length of the chain of tail operations that can be applied to xs. is_list 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}

**primitive**; returns true if x is the empty list null, and false otherwise; time: Theta(1)Theta(1).
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}

**primitive**; returns true if x is a pair and false otherwise; time: Theta(1)Theta(1).
Parameters:
Name Type Description
x 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 null. Laziness: No: is_stream needs to force the given stream.
Parameters:
Name Type Description
xs value given candidate
Returns:
whether xs is a stream
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: Theta(n), space: Theta(1), where n is the length of xs.
Parameters:
Name Type Description
xs list given list
Returns:
length of xs
Type
number

list() → {list}

**primitive**; given n values, returns a list of length n. The elements of the list are the given values in the given order; time: Theta(n)Theta(n).
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: Theta(n), space: Theta(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_stream(xs) → {stream}

Given list xs, returns a stream of same length with the same elements as xs in the same order. Laziness: Yes: list_to_stream goes down the list only when forced.
Parameters:
Name Type Description
xs list given list
Returns:
stream containing all elements of xs
Type
stream

list_to_string(xs) → {string}

Returns a string that represents list xs using the text-based box-and-pointer notation [...]. Iterative process; time: Theta(n) where n is the size of the list, space: Theta(m) where m is the length of the string. The process is iterative, but consumes space O(m) because of the result string.
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: Theta(n) (apart from f), space: Theta(n) (apart from f), 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
Returns:
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
Returns:
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
Returns:
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
Returns:
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
Returns:
p - leading zero bits
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
Returns:
- 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
Returns:
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
Returns:
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
Returns:
- 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
Returns:
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
Returns:
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
Returns:
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
Returns:
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
Returns:
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: Theta(n), space: Theta(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}

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

parse(x) → {value}

returns the parse tree that results from parsing the string str as a Source program. The format of the parse tree is described in chapter 4 of the textbook in Structure and Interpretation of Computer Programs, JavaScript Adaptation (SICP).
Parameters:
Name Type Description
x string given program as a string
Returns:
parse tree
Type
value

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
i number radix
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
Returns:
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: Theta(n), space: Theta(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: Theta(n), space: Theta(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: Theta(n), space: Theta(n), where n is the length of xs. The process is iterative, but consumes space Theta(n) because of the result list.
Parameters:
Name Type Description
xs list given list
Returns:
xs in reverse
Type
list

set_head(p, x) → {undefined}

changes the pair p such that its head is x.
Parameters:
Name Type Description
p pair given pair
x value given value
Returns:
undefined
Type
undefined

set_tail(p, x) → {undefined}

changes the pair p such that its tail is x.
Parameters:
Name Type Description
p pair given pair
x value given value
Returns:
undefined
Type
undefined

stream() → {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 list is generated, and then a stream using list_to_stream is generated from it.
Parameters:
Name Type Description
value1,value2,...,value_n value given values
Returns:
stream containing all values
Type
stream

stream_append(xs, ys) → {stream}

Returns a stream that results from appending the stream ys to the stream xs. In the result, null 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
xs stream given first stream
ys stream given second stream
Returns:
result of appending xs and ys
Type
stream

stream_filter(pred, xs) → {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
pred function unary function returning boolean value
xs 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
f function unary
xs stream given stream
Returns:
true
Type
boolean

stream_length(xs) → {number}

Returns the length of the stream xs. Iterative process. Lazy? No: The function needs to explore the whole stream
Parameters:
Name Type Description
xs stream given stream
Returns:
length of xs
Type
number

stream_map(f, xs) → {stream}

Returns a stream that results from stream xs by 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
f function unary
xs stream given stream
Returns:
result of mapping
Type
stream

stream_member(v, xs) → {stream}

Returns first postfix substream whose head is identical to v (using ===); returns null 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
v value given value
xs stream given stream
Returns:
postfix substream that starts with v
Type
stream

stream_ref(xs, 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
xs stream given stream
n number 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
v value given value
xs 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
v value given value
xs 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
xs stream given stream
Returns:
xs in reverse
Type
stream

stream_tail(xs) → {Stream}

assumes that the tail (second component) of the pair {x} is a nullary function, and returns the result of applying that function. Throws an exception 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
xs Stream given stream
Returns:
result stream (if stream discipline is used)
Type
Stream

stream_to_list(xs) → {list}

Given stream xs, returns a list of same length with the same elements as xs in the same order. Laziness: No: stream_to_list needs to force the whole stream.
Parameters:
Name Type Description
xs stream stream
Returns:
containing all elements of xs
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}

**primitive**; returns tail (second component of given pair p; time: Theta(1)Theta(1).
Parameters:
Name Type Description
p pair given pair
Returns:
tail of p
Type
value

tokenize(x) → {list}

returns the list of tokens that results from lexing the string str
Parameters:
Name Type Description
x string given program as a string
Returns:
list of tokens
Type
list