4 Intermediate Student with Lambda
program | | = | | def-or-expr ... |
| | | | |
def-or-expr | | = | | definition |
| | | | | expr |
| | | | | test-case |
| | | | | library-require |
| | | | |
definition | | = | | (define (name variable variable ...) expr) |
| | | | | (define name expr) |
| | | | | (define-struct name (name ...)) |
| | | | |
expr | | = | | (lambda (variable variable ...) expr) |
| | | | | (λ (variable variable ...) expr) |
| | | | | (local [definition ...] expr) |
| | | | | (letrec ([name expr] ...) expr) |
| | | | | (let ([name expr] ...) expr) |
| | | | | (let* ([name expr] ...) expr) |
| | | | | (expr expr expr ...) |
| | | | | (cond [expr expr] ... [expr expr]) |
| | | | | (cond [expr expr] ... [else expr]) |
| | | | | (if expr expr expr) |
| | | | | (and expr expr expr ...) |
| | | | | (or expr expr expr ...) |
| | | | | (time expr) |
| | | | | name |
| | | | | prim-op |
| | | | | ’quoted |
| | | | | `quasiquoted |
| | | | | number |
| | | | | string |
| | | | | character |
| | | | |
quoted | | = | | name |
| | | | | number |
| | | | | string |
| | | | | character |
| | | | | (quoted ...) |
| | | | | ’quoted |
| | | | | `quoted |
| | | | | ,quoted |
| | | | | ,@quoted |
| | | | |
quasiquoted | | = | | name |
| | | | | number |
| | | | | string |
| | | | | character |
| | | | | (quasiquoted ...) |
| | | | | ’quasiquoted |
| | | | | `quasiquoted |
| | | | | ,expr |
| | | | | ,@expr |
| | | | |
test-case | | = | | (check-expect expr expr) |
| | | | | (check-within expr expr expr) |
| | | | | (check-member-of expr expr ...) |
| | | | | (check-range expr expr expr) |
| | | | | (check-error expr expr) |
| | | | | (check-error expr) |
| | | | |
library-require | | = | | (require string) |
| | | | | (require (lib string string ...)) |
| | | | | (require (planet string package)) |
| | | | |
package | | = | | (string string number number) |
An name or a variable is a sequence of characters
not including a space or one of the following:
" , ' `
( ) [ ]
{ } | ;
#
A number is a number such as 123, 3/2, or
5.5.
A symbol is a quote character followed by a name. A
symbol is a value, just like 0 or empty.
A string is enclosed by a pair of ". Unlike
symbols, strings may be split into characters and manipulated by a
variety of functions. For example, "abcdef",
"This is a string", and "This is a string with \" inside" are all strings.
A character begins with #\ and has the
name of the character. For example, #\a, #\b,
and #\space are characters.
4.1 Pre-Defined Variables
The empty list.
The true value.
The false value.
4.2 Syntax for Intermediate with Lambda
(lambda (variable variable ...) expression) |
Creates a function that takes as many arguments as given variables,
and whose body is expression.
(λ (variable variable ...) expression) |
The Greek letter
λ is a synonym for
lambda.
(expression expression expression ...) |
Calls the function that results from evaluating the first
expression. The value of the call is the value of function’s body when
every instance of name’s variables are replaced by the values of the
corresponding expressions.
The function being called must come from either a definition appearing before the
function call, or from a lambda expression. The number of argument
expressions must be the same as the number of arguments expected by
the function.
(local [definition ...] expression) |
Groups related definitions for use in
expression. Each
definition can be either a
define or a
define-struct.
When evaluating
local, each
definition is evaluated
in order, and finally the body
expression is
evaluated. Only the expressions within the
local (including
the right-hand-sides of the
definitions and the
expression) may refer to the names defined by the
definitions. If a name defined in the
local is the
same as a top-level binding, the inner one “shadows” the outer
one. That is, inside the
local, any references to that name
refer to the inner one.
(letrec ([name expr-for-let] ...) expression) |
Like
local, but with a simpler syntax. Each
name
defines a variable (or a function) with the value of the
corresponding
expr-for-let. If
expr-for-let is a
lambda,
letrec defines a function, otherwise it
defines a variable.
(let* ([name expr-for-let] ...) expression) |
Like
letrec, but each
name can only be used in
expression, and in
expr-for-lets occuring after
that
name.
(let ([name expr-for-let] ...) expression) |
Like
letrec, but the defined
names can be used only
in the last
expression, not the
expr-for-lets
next to the
names.
Measures the time taken to evaluate
expression. After
evaluating
expression,
time prints out the time
taken by the evaluation (including real time, time taken by the
CPU, and the time spent collecting free memory). The value of
time is the same as that of
expression.
4.3 Common Syntaxes
The following syntaxes behave the same in the Intermediate with Lambda
level as they did in the Intermediate Student level.
(define (name variable variable ...) expression) |
Defines a function named name. The expression is the body
of the function. When the function is called,
the values of the arguments are inserted into the body in place of the
variables. The function returns the value of that new expression.
The function name’s cannot be the same as that of another function or
variable.
Defines a variable called name with the the value of
expression. The variable name’s cannot be the same as that of
another function or variable, and name itself must not appear in
expression.
Defines a new structure called
structure-name. The structure’s fields are
named by the
field-names. After the
define-struct, the following new
functions are available:
make-structure-name : takes a number of
arguments equal to the number of fields in the structure,
and creates a new instance of that structure.
structure-name-field-name : takes an
instance of the structure and returns the value in the field named by
field-name.
structure-name? : takes any value, and returns
true if the value is an instance of the structure.
The name of the new functions introduced by define-struct
must not be the same as that of other functions or variables,
otherwise define-struct reports an error.
(cond [question-expression answer-expression] ...) |
(cond [question-expression answer-expression] | ... | [else answer-expression]) |
|
Chooses a clause based on some condition.
cond finds the first
question-expression that evaluates to
true, then
evaluates the corresponding
answer-expression.
If none of the question-expressions evaluates to true,
cond’s value is the answer-expression of the
else clause. If there is no else, cond reports
an error. If the result of a question-expression is neither
true nor false, cond also reports an error.
(if test-expression then-expression else-expression) |
When the value of the
test-expression is
true,
if evaluates the
then-expression. When the test is
false,
if evaluates the
else-expression.
If the
test-expression is neither
true nor
false,
if reports an error.
(or expression expression expression ...) |
Evaluates to
true if all the
expressions are
true. If any
expression is
false, the
or
expression immediately evaluates to
false (and the expressions to the
right of that expression are not evaluated.)
If any of the expressions evaluate to a value other than
true or
false,
or reports an error.
(and expression expression expression ...) |
Evaluates to
true as soon as one of the
expressions is
true (and the expressions to the right of that
expression are not evaluated.) If all of the
expressions are
false,
the
and expression evaluates to
false.
If any of the expressions evaluate to a value other than
true or
false,
and reports an error.
Checks that the first expression evaluates to the same value as the
expected-expression.
(check-within expression expected-expression delta-expression) |
Checks that the first
expression evaluates to a value within
delta-expression of the
expected-expression. If
delta-expression is not a number,
check-within reports an
error.
Checks that the expression reports an error,
where the error messages matches the
value of matchexpression, if it is present.
Checks that the value of the first expression as that of
one of the following expressions.
(check-range expression low-expression high-expression) |
Checks that the value of the first expression is a number in
between the value of the low-expression and the
high-expression, inclusive.
Makes the definitions of the module specified by string
available in the current module (i.e., the current file), where
string refers to a file relative to the current file.
The string is constrained in several ways to avoid
problems with different path conventions on different platforms: a
/ is a directory separator, . always means the
current directory, .. always means the parent directory,
path elements can use only a through z
(uppercase or lowercase), 0 through 9,
-, _, and ., and the string cannot be
empty or contain a leading or trailing /.
Accesses a file in an installed library. The library name is an
identifier with the same constraints as for a relative-path string
(though without the quotes), with the additional constraint that it
must not contain a ..
Accesses a file in an installed library, making its definitions
available in the current module (i.e., the current file). The first
string names the library file, and the remaining
strings name the collection (and sub-collection, and so on)
where the file is installed. Each string is constrained in the same
way as for the
(require string) form.
(require (planet string (string string number number))) |
Accesses a library that is distributed on the internet via the
PLaneT server, making it definitions available in the current module
(i.e., current file).
4.4 Pre-defined Functions
4.4.1 Numbers: Integers, Rationals, Reals, Complex, Exacts, Inexacts
< : (real real real ... -> boolean) |
Compares real numbers for less-than.
<= : (real real real ... -> boolean) |
Compares real numbers for less-than or equality.
= : (number number number ... -> boolean) |
Compares numbers for equality.
> : (real real real ... -> boolean) |
Compares real numbers for greater-than.
>= : (real real ... -> boolean) |
Compares real numbers for greater-than or equality.
Evaluates the absolute value of a real number.
acos : (number -> number) |
Evaluates the arccosine (inverse of cos) of a number.
add1 : (number -> number) |
Evaluates a number one larger than a given number.
Extracts the angle from a complex number.
asin : (number -> number) |
Evaluates the arcsine (inverse of sin) of a number.
atan : (number (number) -> number) |
Evaluates the arctan of the given number or the ratio of the two given numbers.
Determines the closest integer (exact or inexact) above a real number.
Determines whether some value is complex.
Evaluates the conjugate of a complex number.
Evaluates the cosine of a number (radians).
cosh : (number -> number) |
Evaluates the hyperbolic cosine of a number.
Evaluates the current time in seconds elapsed (since a platform-specific starting date).
Evaluates the denominator of a rational.
Euler’s number.
even? : (integer -> boolean) |
Determines if some integer (exact or inexact) is even or not.
Converts an exact number to an inexact one.
Determines whether some number is exact.
Evaluates e raised to a number.
expt : (number number -> number) |
Evaluates the power of the first to the second number.
floor : (real -> integer) |
Determines the closest integer (exact or inexact) below a real number.
gcd : (integer integer ... -> integer) |
Evaluates the greatest common divisior of two integers (exact or inexact).
Extracts the imaginary part from a complex number.
Approximates an inexact number by an exact one.
Determines whether some number is inexact.
Lookups the character that corresponds to the given integer (exact only!) in the ASCII table (if any).
Evaluates the integer (exact or inexact) square root of a number.
Determines whether some value is an integer (exact or inexact).
lcm : (integer integer ... -> integer) |
Evaluates the least common multiple of two integers (exact or inexact).
Evaluates the base-e logarithm of a number.
Determines the magnitude of a complex number.
Creates a complex from a magnitude and angle.
Creates a complex from a real and an imaginary part.
max : (real real ... -> real) |
Determines the largest number.
min : (real real ... -> real) |
Determines the smallest number.
modulo : (integer integer -> integer) |
Finds the remainder of the division of the first number by the second; try (modulo 4 3) (modulo 4 -3).
Determines if some value is strictly smaller than zero.
Converts a number to a string.
Determines whether some value is a number.
Evaluates the numerator of a rational.
odd? : (integer -> boolean) |
Determines if some integer (exact or inexact) is odd or not.
The ratio of a circle’s circumference to its diameter.
Determines if some value is strictly larger than zero.
Divides the first integer (exact or inexact) into the second; try (quotient 3 4) and (quotient 4 3).
Generates a random natural number less than some given integer (exact only!).
Determines whether some value is a rational number.
Extracts the real part from a complex number.
Determines whether some value is a real number.
Determines the remainder of dividing the first by the second integer (exact or inexact).
round : (real -> integer) |
Rounds a real number to an integer (rounds to even to break ties).
sgn : (real -> (union 1 1.0 0 0.0 -1 -1.0)) |
Evaluates the sign of a real number.
Evaluates the sine of a number (radians).
sinh : (number -> number) |
Evaluates the hyperbolic sine of a number.
Evaluates the square of a number.
sqrt : (number -> number) |
Evaluates the square root of a number.
sub1 : (number -> number) |
Evaluates a number one smaller than a given number.
Evaluates the tangent of a number (radians).
zero? : (number -> boolean) |
Determines if some value is zero or not.
4.4.2 Booleans
Determines whether two booleans are equal.
Determines whether some value is a boolean.
Determines whether a value is false.
not : (boolean -> boolean) |
Evaluates the negation of a boolean value.
4.4.3 Symbols
Converts a symbol to a string.
Determines whether two symbols are equal.
Determines whether some value is a symbol.
4.4.4 Lists
append : ((listof any) ... -> (listof any)) |
Creates a single list from several, by juxtaposition of the items.
assq | : | (X | (listof (cons X Y)) | -> | (union false (cons X Y))) |
|
|
Determines whether some item is the first item of a pair in a list of pairs.
caaar | : | ((cons | (cons (cons W (listof Z)) (listof Y)) | (listof X)) | -> | W) |
|
|
Selects the first item of the first list in the first list of a list.
caadr | : | ((cons | (cons (cons W (listof Z)) (listof Y)) | (listof X)) | -> | (listof Z)) |
|
|
Selects the rest of the first list in the first list of a list.
caar : ((cons (cons Z (listof Y)) (listof X)) -> Z) |
Selects the first item of the first list in a list.
cadar | : | ((cons (cons W (cons Z (listof Y))) (listof X)) | -> | Z) |
|
|
Selects the second item of the first list of a list.
Selects the fourth item of a non-empty list.
caddr : ((cons W (cons Z (cons Y (listof X)))) -> Y) |
Selects the third item of a non-empty list.
cadr : ((cons Z (cons Y (listof X))) -> Y) |
Selects the second item of a non-empty list.
car : ((cons Y (listof X)) -> Y) |
Selects the first item of a non-empty list.
cdaar | : | ((cons | (cons (cons W (listof Z)) (listof Y)) | (listof X)) | -> | (listof Z)) |
|
|
Selects the rest of the first list in the first list of a list.
cdadr | : | ((cons W (cons (cons Z (listof Y)) (listof X))) | -> | (listof Y)) |
|
|
Selects the rest of the first list in the rest of a list.
cdar | : | ((cons (cons Z (listof Y)) (listof X)) | -> | (listof Y)) |
|
|
Selects the rest of a non-empty list in a list.
cddar | : | ((cons (cons W (cons Z (listof Y))) (listof X)) | -> | (listof Y)) |
|
|
Selects the rest of the rest of the first list of a list.
cdddr | : | ((cons W (cons Z (cons Y (listof X)))) | -> | (listof X)) |
|
|
Selects the rest of the rest of the rest of a list.
cddr : ((cons Z (cons Y (listof X))) -> (listof X)) |
Selects the rest of the rest of a list.
cdr : ((cons Y (listof X)) -> (listof X)) |
Selects the rest of a non-empty list.
cons : (X (listof X) -> (listof X)) |
Constructs a list.
Determines whether some value is a constructed list.
Selects the eighth item of a non-empty list.
Determines whether some value is the empty list.
fifth : ((listof Y) -> Y) |
Selects the fifth item of a non-empty list.
first : ((cons Y (listof X)) -> Y) |
Selects the first item of a non-empty list.
Selects the fourth item of a non-empty list.
length : ((listof any) -> number) |
Evaluates the number of items on a list.
list : (any ... -> (listof any)) |
Constructs a list of its arguments.
list* : (any ... (listof any) -> (listof any)) |
Constructs a list by adding multiple items to a list.
list-ref : ((listof X) natural-number -> X) |
Extracts the indexed item from the list.
make-list : (natural-number any -> (listof any)) |
Constructs a list of k (the first argument) copies of x (the second argument).
member : (any (listof any) -> boolean) |
Determines whether some value is on the list (comparing values with equal?).
member? : (any (listof any) -> boolean) |
Determines whether some value is on the list (comparing values with equal?).
memq : (any (listof any) -> (union false list)) |
Determines whether some value is on some list if so, it produces the suffix of the list that starts with x if not, it produces false. (It compares values with the eq? predicate.)
memv : (any (listof any) -> (union false list)) |
Determines whether some value is on the list if so, it produces the suffix of the list that starts with x if not, it produces false. (It compares values with the eqv? predicate.)
The empty list.
Determines whether some value is the empty list.
remove : (any (listof any) -> (listof any)) |
Constructs a list like the given one with the first occurrence of the given item removed (comparing values with equal?).
rest : ((cons Y (listof X)) -> (listof X)) |
Selects the rest of a non-empty list.
Creates a reversed version of a list.
second : ((cons Z (cons Y (listof X))) -> Y) |
Selects the second item of a non-empty list.
Selects the seventh item of a non-empty list.
sixth : ((listof Y) -> Y) |
Selects the sixth item of a non-empty list.
third : ((cons W (cons Z (cons Y (listof X)))) -> Y) |
Selects the third item of a non-empty list.
4.4.5 Posns
Constructs a posn.
Signature for posns.
Extracts the x component of a posn.
Extracts the y component of a posn.
posn? : (anything -> boolean) |
Determines if its input is a posn.
4.4.6 Characters
Lookups the number that corresponds to the given character in the ASCII table (if any).
Determines whether a character represents an alphabetic character.
Determines whether a character precedes another (or is equal to it) in a case-insensitive manner.
Determines whether a character precedes another in a case-insensitive manner.
Determines whether two characters are equal in a case-insensitive manner.
Determines whether a character succeeds another (or is equal to it) in a case-insensitive manner.
Determines whether a character succeeds another in a case-insensitive manner.
Determines the equivalent lower-case character.
Determines whether a character is a lower-case character.
Determines whether a character represents a digit.
Determines the equivalent upper-case character.
Determines whether a character is an upper-case character.
Determines whether a character represents space.
char<=? : (char char char ... -> boolean) |
Determines whether a character precedes another (or is equal to it).
char<? : (char char char ... -> boolean) |
Determines whether a character precedes another.
char=? : (char char char ... -> boolean) |
Determines whether two characters are equal.
char>=? : (char char char ... -> boolean) |
Determines whether a character succeeds another (or is equal to it).
char>? : (char char char ... -> boolean) |
Determines whether a character succeeds another.
Determines whether a value is a character.
4.4.7 Strings
explode : (string -> (listof string)) |
Translates a string into a list of 1-letter strings.
format : (string any ... -> string) |
Formats a string, possibly embedding values.
implode : ((listof string) -> string) |
Concatenates the list of 1-letter strings into one string.
Converts an integer in [0,55295] or [57344 1114111] to a 1-letter string.
Converts a s list of characters into a string.
Produces a string of given length from a single given character.
Replicates the given string.
Builds a string of the given characters.
Converts a 1-letter string to an integer in [0,55295] or [57344, 1114111].
Converts a string into a list of characters.
Converts a string into a number, produce false if impossible.
Converts a string into a symbol.
Determines whether all ’letters’ in the string are alphabetic.
Juxtaposes the characters of several strings.
Determines whether one string alphabetically precedes another (or is equal to it) in a case-insensitive manner.
Determines whether one string alphabetically precedes another in a case-insensitive manner.
Compares two strings character-wise in a case-insensitive manner.
Determines whether one string alphabetically succeeds another (or is equal to it) in a case-insensitive manner.
Determines whether one string alphabetically succeeds another in a case-insensitive manner.
Copies a string.
Extracts the ith 1-letter substring from the given one.
Determines the length of a string.
Determines whether all ’letters’ in the string are lower case.
Determines whether all ’letters’ in the string are numeric.
Extracts the i-the character from a string.
Determines whether all ’letters’ in the string are upper case.
Determines whether all ’letters’ in the string are white space.
string<=? : (string string string ... -> boolean) |
Determines whether one string alphabetically precedes another (or is equal to it).
string<? : (string string string ... -> boolean) |
Determines whether one string alphabetically precedes another.
string=? : (string string string ... -> boolean) |
Compares two strings character-wise.
string>=? : (string string string ... -> boolean) |
Determines whether one string alphabetically succeeds another (or is equal to it).
string>? : (string string string ... -> boolean) |
Determines whether one string alphabetically succeeds another.
Determines whether a value is a string.
Extracts the substring starting at a 0-based index up to the second 0-based index (exclusive).
4.4.8 Images
Determines whether two images are equal.
Determines whether a value is an image.
4.4.9 Misc
=~ : (number number non-negative-real -> boolean) |
Checks whether two numbers are within some amount (the third argument) of either other.
The end-of-file value.
Determines whether some value is the end-of-file value.
eq? : (any any -> boolean) |
Determines whether two values are equivalent from the computer’s perspective (intensional).
Determines whether two values are structurally equal where basic values are compared with the eqv? predicate.
equal~? : (any any non-negative-real -> boolean) |
Compares like equal? on the first two arguments, except using =~ in the case of numbers.
eqv? : (any any -> boolean) |
Determines whether two values are equivalent from the perspective of all functions that can be applied to it (extensional).
error : (any ... -> void) |
signals an error, combining the given values into an error message.
If any of the values’ printed representations is too long, it is truncated and “...” is put into the string. If the first value is a symbol, it is treated specially; it is suffixed with a colon and a space (the intention is that the symbol is the name of the function signalling the error).
Exits the running program.
Returns the argument unchanged.
Determines whether some value is a structure.
4.4.10 Numbers (relaxed conditions)
* : (number ... -> number) |
Multiplys all given numbers.
+ : (number ... -> number) |
Adds all given numbers.
- : (number ... -> number) |
Subtracts from the first all remaining numbers.
/ : (number ... -> number) |
Divides the first by all remaining numbers.
4.4.11 Higher-Order Functions
andmap : ((X -> boolean) (listof X) -> boolean) |
(andmap p (list x-1 ... x-n)) = (and (p x-1) ... (p x-n))
apply | : | ((X-1 ... X-N -> Y) | X-1 | ... | X-i | (list X-i+1 ... X-N) | -> | Y) |
|
|
Applies a function using items from a list as the arguments.
argmax : ((X -> real) (listof X) -> X) |
Finds the (first) element of the list that maximizes the output of the function.
argmin : ((X -> real) (listof X) -> X) |
Finds the (first) element of the list that minimizes the output of the function.
(build-list n f) = (list (f 0) ... (f (- n 1)))
(build-string n f) = (string (f 0) ... (f (- n 1)))
compose | : | ((Y-1 -> Z) | ... | (Y-N -> Y-N-1) | (X-1 ... X-N -> Y-N) | -> | (X-1 ... X-N -> Z)) |
|
|
Composes a sequence of procedures into a single procedure.
filter : ((X -> boolean) (listof X) -> (listof X)) |
Constructs a list from all those items on a list for which the predicate holds.
foldl : ((X Y -> Y) Y (listof X) -> Y) |
(foldl f base (list x-1 ... x-n)) = (f x-n ... (f x-1 base))
foldr : ((X Y -> Y) Y (listof X) -> Y) |
(foldr f base (list x-1 ... x-n)) = (f x-1 ... (f x-n base))
for-each : ((any ... -> any) (listof any) ... -> void) |
Applies a function to each item on one or more lists for effect only.
map : ((X ... -> Z) (listof X) ... -> (listof Z)) |
Constructs a new list by applying a function to each item on one or more existing lists.
memf | : | ((X -> any) | (listof X) | -> | (union false (listof X))) |
|
|
Produces true if the function given as the first argument produces a non-false value for any item in the second argument.
ormap : ((X -> boolean) (listof X) -> boolean) |
(ormap p (list x-1 ... x-n)) = (or (p x-1) ... (p x-n))
Produces true if the value is a procedure.
quicksort : ((listof X) (X X -> boolean) -> (listof X)) |
Constructs a list from all items on a list in an order according to a predicate.
sort : ((listof X) (X X -> boolean) -> (listof X)) |
Constructs a list from all items on a list in an order according to a predicate.