CSC151.02 2010S Functional Problem Solving : Reference
Primary: [Front Door] [Schedule] - [Academic Honesty] [Instructions]
Current: [Outline] [EBoard] [Reading] [Lab] - [Assignment] [Quiz]
Groupings: [Assignments] [EBoards] [Examples] [Exams] [Handouts] [Labs] [Outlines] [Projects] [Quizzes] [Readings]
References: [A-Z] [By Topic] - [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [CSC151.01 2010S (Weinman)] [CSC151 2009F (Rebelsky)]
Misc: [SamR] [MediaScript] [GIMP]
(=
num1
num2
)
num1
is equal to
num2
(<
num1
num2
...numn
)
num1
is strictly less than
num2
, and
num2
is strictly less than
num3
and so forth.
(<=
num1
num2
...numn
)
num1
is less than or equal to
num2
, and
num2
is less than
or equal to num3
and so forth.
(>
num1
num2
...numn
)
num1
is strictly greater than
num2
, and
num2
is strictly
greater than num3
and so forth.
(>=
num1
num2
...numn
)
num1
is greater than or equal to
num2
, and
num2
is greater than
or equal to num3
and so forth.
(^and
f1
f2
...
fn
)
and
. Creates
a new procedure that, when applied to some values, returns
(and (f1 values
)
(f2 values
)
...
(fn values
)).
(^not
pred?
)
not
. Creates
a new procedure, that, when applied to some values, returns
the opposite of pred?
. That is, (1)
if pred?
returns a truish value when applied
to some parameters, the new procedure returns #f
when applied to those same parameters; (2) if
pred?
returns false when applied to some
parameters, the new procedure returns #t
when
applied to those same parameters.
(^or
f1
f2
...
fn
)
or
. Creates
a new procedure that, when applied to some values, returns
(or (f1 values
)
(f2 values
)
...
(fn values
)).
(and
exp1
exp2
...
expn
)
(caar
lst
)
lst
's first element is a list,
gets the first element of that first element, the
the car
of the car
of lst
. If
lst
is not a list, or its first element
is not a list, reports an error.
(cadr
lst
)
lst
,
the car
of the cdr
of lst
(caddr
lst
)
lst
,
the car
of the cdr
of the cdr
of lst
.
(car
lst
)
lst
.
(cdr
lst
)
lst
but without
the first element. (cadr
lst
)
lst
,
the car
of the cdr
of lst
(ceiling
num
)
num
. That is, round down.
(char?
val
)
val
is a character.
(char->integer
ch
)
ch
's position in the collating
sequence.
(char<?
ch1
ch2
)
ch1
precedes
ch2
in the collating sequence.
Both ch1
and ch2
must be characters.
(char<=?
ch1
ch2
)
ch1
equals
ch2
or if
ch1
precedes
ch2
in the collating sequence.
Both ch1
and ch2
must be characters.
(char=?
ch1
ch2
)
ch1
and
ch2
are the same. Both
ch1
and ch2
must
be characters.
(char>=?
ch1
ch2
)
ch1
and ch2
are equal or if ch1
follows
ch2
in the collating sequence.
Both ch1
and ch2
must be characters.
(char>?
ch1
ch2
)
ch1
follows
ch2
in the collating sequence.
Both ch1
and ch2
must be characters.
(char-alphabetic?
ch
)
ch
(which must be a
character) is an alphabetic character (in English, #\a, #\b, ... #\z,
#\A, #\B, ...., #\Z).
(char-ci<?
ch1
ch2
)
ch1
and ch2
are alphabetic characters (letters), determine if
ch1
naturally precedes
ch2
, ignoring case.
If either is not alphabetic, determine if ch1
precedes
ch2
in the collating sequence.
Both ch1
and ch2
must be characters.
(char-ci<=?
ch1
ch2
)
ch1
and ch2
are alphabetic characters (letters), determine if
ch1
naturally precedes or equals
ch2
, ignoring case.
If either is not alphabetic, determine if ch1
equals
ch2
or if ch1
precedes
ch2
in the collating sequence.
Both ch1
and ch2
must be characters.
(char-ci=?
ch1
ch2
)
ch1
and ch2
are alphabetic characters (letters), determine if
ch1
and ch2
represent
the same letter, ignoring case.
If either is not alphabetic, determine if ch1
follows
ch2
in the collating sequence.
Both ch1
and ch2
must be characters.
(char-ci>=?
ch1
ch2
)
ch1
and ch2
are alphabetic characters (letters), determine if
ch1
naturally follows or equals
ch2
, ignoring case. If either is not
alphabetic, determine if ch1
equals
ch2
or if ch1
follows ch2
in the collating sequence.
Both ch1
and ch2
must be characters.
(char-ci>?
ch1
ch2
)
ch1
and ch2
are alphabetic characters (letters), determine if
ch1
naturally follows
ch2
, ignoring case.
If either is not alphabetic, determine if ch1
follows
ch2
in the collating sequence.
Both ch1
and ch2
must be characters.
(char-downcase
ch
)
ch
is an upper-case character
(#\A, #\B, ... #\Z, in ASCII; potentially other characters in other
character sets), return the corresponding lower-case
character. Otherwise, return the same character.
(char-lower-case?
ch
)
ch
(which must be a
character) represents a lower-case character
(in English, #\a, #\b, ... #\z).
(char-upper-case?
ch
)
ch
(which must be a
character) represents an upper-case character
(in English, #\a, #\b, ... #\z).
(char-numeric?
ch
)
ch
(which must be a
character) represents a digit in a number (traditionally,
#\0, #\1, ... #\9, although other systems have different
numeric characters).
(char-upcase
ch
)
ch
is a lower-case character
(#\a, #\b, ... #\z, in ASCII; potentially other characters in other
character sets), return the corresponding upper-case
character (#\A for #\a, #\B for #\b, etc.). Otherwise, return
the same character.
(char-whitespace?
ch
)
ch
(which must be a
character) represents a whitespace character, such as a space,
a tab, or a newline.
(close-input-port
input-port
)
(close-output-port
output-port
)
(color->rgb
string
)
(color-name?
val
)
val
is a string representing a
color in the database of colors.
(color-name->rgb
string
)
(complex?
val
)
val
can be interpreted
as a complex number.
(compose
f
g
)
((compose f g) x)
is the same as (f (g x))
.
(cons
value
lst
)
value
to the front of lst
.
(constant
value
)
value
,
no matter what parameters it is applied to.
(context-get-bgcolor
)
(context-get-brush
)
(context-get-color-names
)
(context-get-fgcolor
)
(context-list-brushes
)
(context-list-brushes
pattern
)
(context-list-colors
)
(context-list-colors
pattern
)
(context-list-fonts
)
(context-list-fonts
pattern
)
(context-set-bgcolor!
color
)
(context-set-brush!
brush-name
)
(context-set-fgcolor!
color
)
(context-update-displays!
)
(delete-file
filename
)
(display
value
)
value
on the screen.
(display
value
output-port
)
value
on the specified
port.
(drawing?
value
)
value
can be interpreted
as a drawing.
drawing-blank
(drawing-blank?
value
)
value
can be interpreted
as a blank drawing.
(drawing-bottom
drawing
)
(drawing-color
drawing
)
(drawing-compose
list-of-drawings
)
list-of-drawings
. Note that
the drawings are stacked first to last, so the first drawing
in the list is at the bottom and the last in the list is at
the top.
(drawing-group
drawing1
drawing2
...
drawingn
)
drawing1
is at the bottom and
drawingn
is at the top.
(drawing-height
drawing
)
(drawing-hscale
drawing
factor
)
drawing
by factor
. Note that every part of
the drawing is scaled horizontally, including the horizontal
distance of each component of the drawing from the origin.
(drawing-hshift
drawing
amt
)
drawing
horizontally by
factor
. If factor
is positive, the drawing is shifted to the right. If
factor
is negative, the drawing is
shifted left by the absolute value of factor
.
(drawing-join
drawing1
drawing2
)
drawing2
on top of drawing1
.
(drawing-left
drawing
)
(drawing-outline
drawing
brush
)
drawing
with brush
.
Element colors are preserved. After outlining, no elements
of the resulting drawing are filled.
(drawing-recolor
drawing
color
)
drawing
in color
.
Note that even if drawing
contained
colors, the new drawing contains only a single color.
(drawing-right
drawing
)
(drawing-scale
drawing
factor
)
drawing
by factor
. Note that every part of
the drawing is scaled, including both the horizontal and
vertical distance of each component
of the drawing from the origin.
(drawing->image
drawing
width
height
)
drawing
that
fits in the rectangular region bounded on the left by 0, on
the top by 0, on the right by width
-1
and on the bottom by height
-1.
(drawing-top
drawing
)
(drawing-type
drawing
)
ellipse
for ellipses and circles,
rectangle
for rectangles and squares,
group
for grouped drawings, and
line
for lines (not yet support).
drawing-unit-circle
drawing-unit-square
(drawing-vscale
drawing
factor
)
drawing
by factor
. Note that every part of
the drawing is scaled vertically, including the vertical distance
of each component of the drawing from the origin.
(drawing-vshift
drawing
amt
)
drawing
vertically by
factor
. If factor
is positive, the drawing is shifted downward. If
factor
is negative, the drawing is
shifted upward by the absolute value of factor
.
(drawing-width
drawing
)
(eof-object?
val
)
val
is something returned by
read
(or read-char
or
peek-char
) to indicate the end of input.
(error
message
)
(error
message
val1
... valn
)
(even?
int
)
int
is
even (that is, has a remainder of 0 when divided by 2).
(exact?
num
)
num
is
represented exactly (that is, not approximated).
(exact->inexact
num
)
num
.
(expt
base
power
)
base
power
.
(file-exists?
filename
)
(filter
pred?
lst
)
lst
for which
pred?
holds.
(filter
means to “filter in”,
not to “filter out”.)
(floor
num
)
num
. That is, round down.
(for-each
proc!
lst
)
proc!
to each element of the
given list. Called primarily for side effects.
(if
test
consequent
alternative
)
test
. If its value is truish (that is,
anything but false), evaluate consequent
and return its value. If the value of test is false (#f), evaluate
and return alternative
.
(image?
val
)
val
is an image.
(image-blot!
image
col
row
)
(image-calculate-pixels!
image
fun
fun
to
the column and row.
fun
should have the
form (lambda (col row) color)
.
Unlike image-compute-pixels!
, which can compute
pixels in any order, image-calculate-pixels!
is
guaranteed to do a row-by-row, left-to-right scan through
the image. However, image-calculate-pixels!
is likely to be slower than image-compute-pixels!
(although faster than coding the iteration yourself).
(image-compute
pos2color
width
height
)
pos2color
(a function of the form (lambda (col row) color)
)
to compute the color at each position in the image. compute
(image-compute-pixels!
image
pos2color
)
function
to the position of the pixel.
function
must have
the form (lambda (col row) expression-to-compute-color)
.
(image-draw-line!
image
col1
row1
col2
row2
)
(image-fill-selection!
image
)
(image-get-pixel
image
column
row
)
(image-height
image
)
(image-load
filename
)
(image-new
width
height
)
(image-save
image
fname
)
image
in the specified file
(which should provide the full path to the file). The type
of the image (JPEG, GIF, PNG, etc.) is determined by the suffix
of the file name.
(image-scan
image
proc!
)
proc!
the the
column, row, and color at each position. proc!
should have the form (lambda (col row color) ...)
.
In contrast to many of the other image iteration procedures,
image-scan
is guaranteed to scan row-by-row
from top-to-bottom, scanning each row from left to right.
(image-select-all!
image
)
(image-select-ellipse!
image
selection-type
left
top
width
height
)
left
, top margin is
top
, width is width
and height is height
.
If selection-type
is
REPLACE
, the ellipse replaces the
current selection. If selection-type
is ADD
, the ellipse is added to the
current selection. If selection-type
is SUBTRACT
, the ellipse is subtracted from
the current selection. If selection-type
is INTERSECT
, the ellipse is intersected
with the current selection (that is, only points that are in
both the current selection and the ellipse remain selected).
(image-select-inverse!
image
)
(image-select-nothing!
image
)
(image-select-polygon!
image
selection-type
positions
)
position-new
).
If selection-type
is
REPLACE
, the polygon replaces the
current selection. If selection-type
is ADD
, the polygon is added to the
current selection. If selection-type
is SUBTRACT
, the polygon is subtracted from
the current selection. If selection-type
is INTERSECT
, the polygon is intersected
with the current selection (that is, only points that are in
both the current selection and the polygon remain selected).
(image-select-rectangle!
image
selection-type
left
top
width
height
)
left
, top margin is
top
, width is width
and height is height
.
If selection-type
is
REPLACE
, the rectangle replaces the
current selection. If selection-type
is ADD
, the rectangle is added to the
current selection. If selection-type
is SUBTRACT
, the rectangle is subtracted from
the current selection. If selection-type
is INTERSECT
, the rectangle is intersected
with the current selection (that is, only points that are in
both the current selection and the rectangle remain selected).
(image-set-pixel!
image
column
row
rgb-color
)
(image-show
image
)
(image-stroke-selection!
image
)
(image-transform!
image
fun
)
image
in place by setting
each pixel to the result of applying fun
to
that current pixel color.
(image-transform-pixel!
image
column
row
func
)
col
,row
)
in image
by applying
func
to its old color and setting that
pixel to the resulting color.
(image-variant
image
fun
)
image
, each of whose pixels is computed
by applying fun
to the color of the
corresponding pixel in image
.
(image-width
image
)
(inexact?
num
)
num
is
represented inexactly (that is, approximated).
(inexact->exact
num
)
num
.
(Of course, if num
was already
approximated, the result, while exact, still approximates
whatever num
approximated.)
(input-port?
val
)
val
is an open input port.
(integer?
val
)
val
is an integer.
(integer->char
n
)
n
th character in the
collating sequence.
(lambda
(params)
expression1
...
expressionn
)
params
, does the computation indicated
by the expressions, and returns the value of the last expression.
(left-section
binproc
left
)
v
, returns
(binproc
left
v
)
.
(list
val_0
val_1
...
val_n
)
n
+1 of the form
(val_0
val_1
... val_n
)
.
(list->string
char-list
)
char-list
(which must be a
list of characters) to a string.
The ith element of the list becomes the
ith character in the string.
(list->vector
lst
)
lst
to a vector so that
the ith value in the vector is the same as the ith value in the lst.
(list-drop
lst
n
)
n
elements of lst
.
(list-ref
lst
n
)
n
th element of
lst
. Note that elements are numbered
starting at 0.
(list-take
lst
n
)
n
elements of lst
.
(l-s
binproc
left
)
left-section
.
(make-list
n
val
)
n
copies of val
.
(make-string
length
ch
)
length
,
containing only copies of ch
.
(make-vector
length
val
)
length
,
containing only copies of val
.
(map
func
lst
)
func
to the corresponding element of
lst
.
(max
v1
v2
)
v1
and
v2
.
(max
v1
v2
...
vn
)
v1
through vn
.
(min
v1
v2
)
v1
and
v2
.
(min
v1
v2
...
vn
)
v1
through vn
.
(modulo
value
modulus
)
modulus
-sized sections, gives the
offset of value
from the start of its
section.
(negative?
num
)
num
is
negative (less than zero).
(newline
)
(newline
output-port
)
(null?
lst
)
lst
is the empty list.
null
(number?
val
)
val
is a number.
(number->string
num
)
num
to an appropriate textual
representation.
(o
f1
f2
...
fn-1
fn
)
f
, in turn, starting with
fn
and
working backwards. The composition, when applied to a value,
x
, produces the same result as
(f1
(f2
(...
(fn-1
(fn x)))))
.
(odd?
int
)
int
is
odd (that is, has a remainder of 1 when divided by 2).
(open-input-file
filename
)
(open-output-file
filename
)
(or
exp1
exp2
...
expn
)
(output-port?
val
)
val
is an open output port.
(peek-char
)
(peek-char
input-port
)
(position?
value
)
value
is a position (or can
be interpreted as such).
(position-col
pos
)
pos
.
(position-distance
pos1
pos2
)
pos1
and
pos2
.
(position-interpolate
pos1
pos2
percent
)
percent
(expressed as a real number between 0 and 1) of the way from
pos1
to pos2
(position-new
col
row
)
col
,row
).
(position-offset
position
hoff
voff
)
position
horizontally by hoff
and vertically
by voff
.
(position-row
pos
)
(positive?
num
)
num
is
positive (greater than zero).
(quotient
dividend
divisor
)
dividend
and
divisor
, both of which must be integers.
The quotient is the whole part of the result of dividing
dividend
by divisor
.
(random-positions
n
cols
rows
)
n
positions, each of which is
difficult to predict and each with a non-negative column less
than cols
and a non-negative row less
than rows
.
(rational?
val
)
val
can be interpreted
as a rational number.
(read
)
(read
input-port
)
(read-char
)
(read-char
input-port
)
(real?
val
)
val
is a real number.
(region-calculate-pixels!
image
left
top
width
height
fun
fun
at
each position in the region.
fun
should have the
form (lambda (col row) color)
.
Unlike region-compute-pixels!
, which can compute
pixels in any order, region-calculate-pixels!
is
guaranteed to do a row-by-row, left-to-right scan through
the image. However, region-calculate-pixels!
is likely to be slower than region-compute-pixels!
(although faster than coding the iteration yourself).
(region-compute-pixels!
image
left
top
width
height
pos2color
)
function
to the position of the pixel.
function
must be a function of
the form (lambda (col row) expression-to-compute-color)
.
(region-scan
image
left
top
width
height
proc!
)
proc!
at
each position in the region. proc!
should have the
form (lambda (col row color) ...)
. In contrast to
many of the other region procedures, region-scan
is
guaranteed to scan row-by-row from top-to-bottom, scanning each
row from left to right.
(remainder
dividend
divisor
)
dividend
by divisor
.
(repeat
i
proc!
)
proc!
(a zero-parameter procedure,
a.k.a., a “thunk”) i
times.
(repeat
i
proc!
val
)
(proc!
val
)
i
times.
(repeat
i
proc!
v1
...
vn
)
(proc!
v1
...
vn
)
i
times.
(reverse
lst
)
lst
, but in the opposite order.
(rgb?
value
)
value
can be interpreted
as an RGB color.
(rgb->color-name
rgb-color
)
(rgb->rgb-list
rgb-color
)
(rgb->string
rgb-color
)
(rgb-bluer
rgb-color
)
(rgb-blue
color
)
(rgb-complement
rgb-color
)
(rgb-darker
rgb-color
)
(rgb-greener
rgb-color
)
(rgb-green
color
)
(rgb-lighter
rgb-color
)
(rgb-list->rgb
rgb-lst
)
(r g b)
into an RGB color.
(rgb-new
r
g
b
)
(rgb-phaseshift
rgb-color
)
(rgb-redder
rgb-color
)
(rgb-red
color
)
(rgb-rotate
rgb-color
)
rgb-transparent
image-compute-pixels!
(and variants thereof).
If the color function returns rgb-transparent
for a particular position, the color at that position is left
unchanged.
(right-section
binproc
right
)
v
, returns
(binproc
v
right
)
.
(round
num
)
num
toward the nearest integer.
If the decimal portion of num
is greater
than 1/2, rounds up. If the decimal portion is less than
1/2, rounds down. If the decimal portion equals 1/2, may
round in either direction. (In most implementations,
numbers with fractional portions equal to 1/2 round toward
the even number.)
(r-s
binproc
right
)
right-section
.
(selection-compute-pixels!
image
pos2color
)
function
to the
position of the pixel. function
must have the form (lambda (col row)
expression-to-compute-color)
.
(string?
val
)
val
is a string.
(string->number
str
)
"23"
or "3.14"
, or even
"2.11e-5"
), return the corresponding number.
(substring
str
start
end
)
start
to end
-1
of str
. Note that
substring
, like string-ref
uses 0-based indexing.
(string<?
str1
str2
)
str1
lexicographically
precedes str2
.
Both str1
and str2
must be strings.
(string<=?
str1
str2
)
str1
is either the same as
str2
or if str1
lexicographically precedes str2
.
Both str1
and str2
must be strings.
(string=?
str1
str2
)
str1
is the same as
str2
.
Both str1
and str2
must be strings.
(string>=?
str1
str2
)
str1
is either the same as
str2
or if str1
lexicographically follows str2
.
Both str1
and str2
must be strings.
(string>?
str1
str2
)
str1
lexicographically
follows str2
.
Both str1
and str2
must be strings.
(string
ch_0
ch_1
...
ch_n
)
n
+1,
by concatenating all of
ch_0
through ch_n
.
(string-append
str_0
str_1
...
str_n
)
str
through str_n
in order. (Much like append
, but for
strings, rather than lists.)
(string-ci<?
str1
str2
)
str1
lexicographically
precedes str2
, ignoring case.
Both str1
and str2
must be strings.
(string-ci<=?
str1
str2
)
str1
is either the same as
str2
or if str1
lexicographically precedes str2
,
ignoring case.
Both str1
and str2
must be strings.
(string-ci=?
str1
str2
)
str1
is the same as
str2
, ignoring case.
Both str1
and str2
must be strings.
(string-ci>=?
str1
str2
)
str1
is either the same as
str2
or if str1
lexicographically follows str2
, ignoring case.
Both str1
and str2
must be strings.
(string-ci>?
str1
str2
)
str1
lexicographically
follows str2
, ignoring case.
Both str1
and str2
must be strings.
(string-length
str
)
str
.
(string-ref
str
pos
)
list-ref
, string-ref
presupposes zero-based indexing; the position is specified by
the number of characters that precede it in the string. Hence,
the initial character in the string is at position 0, the next
at position 1, and so on.)
(string->list
str
)
str
to a list of characters.
The ith element of the list is the
ith character in the string.
(truncate
num
)
num
.
That is, round toward zero.
(turtle-clone
turtle
)
turtle
(same position, direction, color, brush, etc.).
(turtle-down!
turtle
)
turtle
's brush down. When the turtle
moves forward, it draws with the brush.
(turtle-face!
turtle
angle
)
turtle
face the diretion specified by
angle
(clockwise
from right).
(turtle-forward!
turtle
distance
)
turtle
forward by the specified distance.
(turtle-new
image
)
image
.
(turtle-set-brush!
turtle
brush
)
turtle
draws with.
(turtle-set-color!
turtle
color
)
turtle
draws.
(turtle-teleport!
turtle
col
row
)
turtle
to
(col
,row
).
Do not draw along the way.
(turtle-turn!
turtle
angle
)
turtle
clockwise by
angle
degrees.
(turtle-up!
turtle
)
turtle
's brush. When
turtle
moves forward, it
will not draw.
(turtle-world
turtle
)
turtle
resides.
(usleep
usec
)
(vector
val_0
val_1
...
val_n
)
n
+1 of the form
#(val_0
val_1
... val_n
)
.
(vector?
val
)
val
is a vector.
(vector->list
vec
)
vec
to a list so that
the ith value in the list is the same as the ith value in the vector.
(vector-fill!
vec
val
)
vec
with multiple copies of
val
.
(vector-length
vec
)
vec
.
(vector-ref
vec
n
)
n
th element of
vec
. Note that elements are numbered
starting at 0.
(vector-set!
vec
k
val
)
k
th element of
vec
to val
.
(Note that vectors use 0-based indexing.)
(when
test
exp1
exp2
...
expn
)
test
. If it holds, evaluate
each expression in turn. Otherwise, do nothing.
(write
value
)
(write
value
output-port
)
(write-char
ch
)
(write-char
ch
output-port
)
(zero?
num
)
num
is
zero.
Primary: [Front Door] [Schedule] - [Academic Honesty] [Instructions]
Current: [Outline] [EBoard] [Reading] [Lab] - [Assignment] [Quiz]
Groupings: [Assignments] [EBoards] [Examples] [Exams] [Handouts] [Labs] [Outlines] [Projects] [Quizzes] [Readings]
References: [A-Z] [By Topic] - [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [CSC151.01 2010S (Weinman)] [CSC151 2009F (Rebelsky)]
Misc: [SamR] [MediaScript] [GIMP]
Copyright (c) 2007-10 Janet Davis, Matthew Kluber, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials copyright by John David Stone and Henry Walker and used by permission.)
This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.
This work is licensed under a Creative Commons
Attribution-NonCommercial 2.5 License. To view a copy of this
license, visit http://creativecommons.org/licenses/by-nc/2.5/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.