So: now that we're getting serious about programming, it seems
appropriate to enumerate all of the procedures that we know how to use.
There aren't so terribly many, are there?
Special Forms
define
(define {variable-name} {value})
(define ({name} {arg1} {arg2} ...)
{body})
(define {name}
(lambda ({arg1} {arg2} ...)
{body}))
|
Bind a value (or function) to a variable (or procedure) name. Note:
the second and third examples are functionally identical.
|
if
(if {predicate}
{consequent}
{alternative})
|
To evaluate an if expression, the interpreter starts by evaluating the
predicate part of the expression. If predicate evaluates
to a true value, the interpreter evaluates the consequent and
returns its value. Otherwise it evaluates the alternative and
returns its value.
|
cond
(cond ({p1} {e1})
({p2} {e2})
...
(else {en}))
|
Conditional expressions are evaluated as follows. The predicate
p1 is evaluated first. If its value is false, then p2
is evaluated. If p2's value is also false, then p3 is
evaluated. This process continues until a predicate {pj} is found whose
value is true, in which case the interpreter returns the value of the
corresponding consequent {ej} of the clause as the value of the
conditional expression.
|
lambda
(lambda ({arg1} {arg2} ...)
{body})
|
|
let
(let (({var1} {value1})
({var2} {value2})
...)
{body})
|
|
Boolean Predicates
Boolean predicates are boolean expressions that take other boolean
expressions as arguments.
(and {e1} {e2} ...)
|
Return #t iff all the expressions evaluate to #t. Note: This is a
special form, but we'll talk about that later.
|
(or {e1} {e2} ...)
|
Return #t if at least on of the expressions evaluates to #t. Note:
This is a special form, but we'll talk about that later.
|
(not {e})
|
Return #t if e is #f, otherwise return #f.
|
Numeric Predicates
Numeric predicates are boolean expressions that take numeric expressions as
arguments.
(= {e1} {e2} ...)
|
Return #t iff all the expressions evaluate to the same value
|
(< {e1} {e2} ...)
(<= {e1} {e2} ...)
(> {e1} {e2} ...)
(>= {e1} {e2} ...)
|
...
|
(even? {e1})
(odd? {e1})
(divides? {numerator}
{denominator})
|
...
|
Numeric Operators
Numeric operators are numeric expressions that take numeric expressions as
arguments.
(+ {e1} {e2} ...)
(- {e1} {e2})
(* {e1} {e2} ...)
(/ {e1} {e2})
(abs {e})
(exp {e})
(log {e})
(max {e1} {e2} ...)
(min {e1} {e2} ...)
(quotient {number}
{divisor})
(remainder {number}
{divisor})
(sqrt {e})
|
Return the result of performing the given mathematical operation.
- and / have special meaning when given only one argument: (- 10) = -10
and (/ 10) = 1/10.
|
Lists and Cons cells
The procedures deal with building and accessing compound data types: lists
and cons cells.
(cons {first} {rest})
|
Construct a cons-cell with {first} as the "car" and {rest} as the
"cdr".
|
(car {cons-cell})
|
Get the "first" part of {cons-cell}.
|
(cdr {cons-cell})
|
Get the "rest" part of {cons-cell}.
|
(list {e1} {e2} ... {en})
|
Build a proper list of all the elements. The list will be a chain of
n cons-cells, where the "car" of each points to one element, and
the "cdr" of each points to the next cons-cell. The "cdr" of the last
cons-cell is '().
|
(null? {thing})
|
Return #t if {thing} is nil (= '()), #f otherwise.
|
(pair? {thing})
|
Return #t if {thing} is a cons-cell, #f otherwise.
|
Other Scheme Procedures
(display {e})
|
Display the result of evaluating {e}, in the interpreter window.
|
(newline)
|
Display a line-feed (i.e. go on to the next line.)
|
(string->list {string})
|
Convert the string to a list of characters:
(string->list "foo") is '(#\f #\o #\o)
|
(char=? {char1} {char2})
|
Return #t if the two operands evaluate to the same character. Uppercase
versus lowercase does matter.
|