Predeclared in Python §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) 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 , the ratio of a circle’s circumference to its radius, which is approximately 6.283185307179586.
Type:
  • float

Functions

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
xs linked_list given linked list
ys linked_list given linked list
Returns:
linked list that results from appending ys to xs
Type
linked_list

apply_in_underlying_python(f, xs) → {value}

calls the function f with arguments given in linked list xs. For example:
def times(x, y):
    return x * y

apply_in_underlying_python(times, list(2, 3)); # returns 6
Parameters:
Name Type Description
f function function to be applied
xs linked_list arguments given in list
Returns:
whatever f returns
Type
value

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
fun function given unary function
n 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
f function given unary function
n int given integer
Returns:
resulting stream
Type
stream

draw_data(value1,)

PRIMITIVE Visualizes the arguments in a separate drawing area in the Source Academy using box-and-pointer diagrams.
Parameters:
Name Type Description
value1, value value2, ...values - given values

enum_llist(start, end) → {linked_list}

Makes a linked list with elements from start to end (inclusive).
Parameters:
Name Type Description
start int starting element of the linked list
end 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
start integer start - starting integer
end integer end - ending integer
Returns:
stream from start to end
Type
stream

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
s stream given stream
n 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
pred function given one-argument function
xs 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
fun function given unary function
xs linked_list given linked list
PRIMITIVE Returns head (first component) of given pair p.
Parameters:
Name Type Description
p pair given pair
Returns:
head of p
Type
value

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
start integer start - starting integer
Returns:
infinite stream from n
Type
stream

is_list(x) → {boolean}

PRIMITIVE returns True if x is a list and False otherwise.
Parameters:
Name Type Description
x 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
xs value given value
Returns:
whether xs is a linked list
Type
boolean

is_none(x) → {boolean}

PRIMITIVE Returns True if x is the empty linked list None, and False otherwise.
Parameters:
Name Type Description
x value given value
Returns:
whether x is None
Type
boolean

is_pair(x) → {boolean}

PRIMITIVE 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_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
xs value given value
Returns:
whether xs is a stream
Type
boolean

length(xs) → {int}

Returns the length of the linked list xs.
Parameters:
Name Type Description
xs 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
x list given value
Returns:
current length of list
Type
int

llist(value1,) → {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
value1, value value2, ...values - given values
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
xs linked_list given linked list
n 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
xs 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
xs 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
f function given unary function
xs 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
x 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
x 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
x 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
x 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
x 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
y int | float The y-coordinate of the point.
x 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
x 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
x 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
x 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
n int Total number of items (must be a non-negative integer).
k 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
x int | float The value whose magnitude (absolute value) will be used.
y 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
x 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
x 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
x 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
x 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
x 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
x 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
x 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
x 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
x 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
n 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
x 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
x int | float The first multiplicand. It is multiplied by y.
y int | float The second multiplicand. It is multiplied by x.
z 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
x int | float The dividend. It will be converted to a float if necessary.
y 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
x 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
*integers 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
x 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
x 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
x 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
n 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
*integers 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
x int | float A numeric value (the significand). It is converted to float if necessary.
i 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
x 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
x int | float The numeric value for which to compute the logarithm.
base(optional) 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
x 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
x 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
x 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
x int | float The starting floating-point number from which the stepping begins.
y int | float The target value that determines the direction. The function will return a value toward y from x.
steps 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
n int Total number of items (must be a non-negative integer).
k 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
x int | float The base value. Both x and y are converted to float before the operation.
y 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
x 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
x int | float The dividend. It will be converted to a float if necessary.
y 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
x 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
x 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
x 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
x 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
x 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
x 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
x 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

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
v value given value
xs 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

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
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 Python 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 str given program as a string
Returns:
parse tree
Type
value

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

Applies binary function f to the elements of xs from right-to-left order.
Parameters:
Name Type Description
f function given binary function
initial value initial value for the accumulation
xs 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
v value given value
xs 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
v value given value
xs linked_list given linked list
Returns:
linked list that results from removing all items from xs that are identical to v
Type
linked_list

reverse(xs) → {linked_list}

Returns linked list xs in reverse order.
Parameters:
Name Type Description
xs linked_list given linked list
Returns:
linked list xs in reverse order
Type
linked_list

set_head(p, x) → {None}

changes the pair p such that its head is x.
Parameters:
Name Type Description
p pair given value
x 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
p pair given value
x value given value
Returns:
None
Type
None

stream(…value1,) → {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
value1, value <repeatable>
value2, ...values - 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 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
xs stream given first stream
ys 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
pred function given pred - unary function returning boolean value
s 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 given unary function
xs 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
xs 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
f function given unary function
xs 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
x value given value
s 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
s stream given stream
n 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
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} 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
xs 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
xs 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
p pair given pair
Returns:
tail of p
Type
value

tokenize(s) → {linked_list}

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