Laboratory: Characters and Strings in Scheme
Summary:
In these exercises, you will explore a number of the standard Scheme
procedures for handling characters and strings. You will also explore
an application of these procedures for marking up text.
Useful Procedures and Notation
Constant notation: #\ch (character
constants) "string" (string constants).
Character constants:
#\a (lowercase a) ... #\z (lowercase z);
#\A (uppercase A) ... #\Z (uppercase
Z); #\0 (zero) ... #\9 (nine);
#\space (space); #\newline (newline); and
#\? (question mark).
Character conversion:
char->integer,
integer->char,
char-downcase, and
char-upcase
Character predicates:
char?,
char-alphabetic?,
char-numeric?,
char-lower-case?,
char-upper-case?,
char-whitespace?,
char<?,
char<=?,
char=?,
char>=?,
char>?,
char-ci<?,
char-ci<=?,
char-ci=?,
char-ci>=?,
and char-ci>?.
String predicates:
string?
String constructors:
make-string,
string,
string-append
String extractors:
string-ref,
substring
String conversion:
list->string,
number->string,
string->list
String analysis:
string-length,
String comparison:
string<?,
string<=?,
string=?,
string>=?,
string>?,
string-ci<?,
string-ci<=?,
string-ci=?,
string-ci>=?,
string-ci>?
Preparation
a. If you have not done so already, you may also want to open separate
tabs in your Web browser with the reading on characters and strings.
b. If you have not done so already, you may want to skim
Section 6.3.5 of the Scheme Report.
c. You may find it helpful to open
the reference page on character procedures and
the reference page on string procedures in separate tabs or windows.
d. No images needed in today's lab!
Exercises
Exercise 1: Collating Sequences
As you may recall, Scheme uses a collating sequence
for the letters, assigning a sequence number to each letter.
Many implementations of Scheme, including MediaScript, use the ASCII
collating sequence.
a. Determine the ASCII collating-sequence numbers for the capital
letter A and for the lower-case letter a.
b. Find out what ASCII character is in position 38 in the collating
sequence.
c. Do the digit characters precede or
follow the capital letters in the ASCII collating
sequence?
d. If you were designing a character set, where in the collating
sequence would you place the space character? Why?
e. What position does the space character occupy in ASCII?
Exercise 2: Character Predicates
a. Determine whether our implementation of Scheme considers
#\newline a whitespace character.
b. Determine whether our implementation of Scheme indicates that
capital B precedes or follows lower-case a.
c. Verify that the case-insensitive comparison operation,
char-ci<?, gives the expected result for the
previous comparison.
d. Determine whether our implementation of Scheme indicates that
#\a and #\A are the same letter. (It should
not.)
e. Find an equality predicate that returns
#t when given #\a and #\A
as parameters.
Exercise 3: String Basics
a. Write a Scheme expression to determine whether the symbol
'plaid is a string.
b. Write a Scheme expression to determine whether the character
#\A is a string.
c. Does the empty string (represented as "") count as a string?
Exercise 4: Creating Questions
Develop three ways of constructing the string "???":
one using a call to make-string,
one a call to string, and one a call to
list->string.
Exercise 5: Referencing Lengths
Here are two opposing views about the relationship between
string-length and string-ref:
No matter what string str is, provided that it's not the
empty string, (string-ref str (string-length str)) will return
the last character in the string.
No matter what string str is, (string-ref str
(string-length str)) is an error.
Which, if either, of these views is correct? Why?
Exercise 6: Building Simple Sentences
Consider the definition
(define like
(string-append
"I like "
person
" because "
person
" is "
adjective
"."))
a. What other values must be defined in order for this definition to work?
b. What type must those values have?
c. Suppose you had previously defined person as
"Ms. Davis" and adjective as
"cheerful". What do you expect the value of
like to be?
d. Check your previous answer experimentally.
e. Write a procedure that does the same thing as this definition.
(That is, takes a person and an adjective as inputs and produces
a string that describes the person.)
Exercise 7: Building Sentences, Revisited
One criticism of the like definition in the previous exercise
is that it takes a lot of lines. We could define a similar sentence as
follows:
(define tunes
(string-append "I listen to " band " because their music is " adjective "."))
a. What are the comparative advantages and disadvantages of the single-line
sentence-building definition?
b. Define band and adjective in such a way that
tunes can be successfully defined.
c. Write a procedure that behaves like this definition.
Exercise 8: A Simple Form Letter
We can, of course, use a similar technique to build longer form letters.
Consider the following definitions
(define cr (string #\newline))
(define letter
(string-append
"Dear " recipient ", " cr
cr
"Thank you for your submission to " magazine ". Unfortunately, we " cr
"consider the subject of your article, " article ", inappropriate for our" cr
"readership. In fact, it is probably inappropriate for any readership." cr
"Please do not contact us again, and do not bother other magazines with" cr
"this inappropriate material or we will be forced to contact the " cr
"appropriate authorities." cr
cr
"Regards," cr
"Ed I. Tor" cr))
a. What must be defined for the definition of letter to
succeed?
b. Check that the definition of letter
works by using the following associated definitions.
(define recipient "Professor Schneider")
(define magazine "College Trustee News")
(define article "Using Grinnell's Endowment to Eliminate Tuition")
c. You may note that the output is fairly ugly when you simply ask for
letter. You can get nicer output by using the
display
procedure, as in (display letter). Try doing so.
Exercise 9: Adding Quotation Marks
a. What changes are necessary to letter so that name
of the article appears in quotation marks?
b. Check your answer experimentally.
Exercise 10: More Form Letters
a. Create a file, sam.scm, with the following lines:
(define recipient "Mr. Rebelsky")
(define magazine "Liberal Arts Letters")
(define article "Why Every Faculty Member Should Take Introductory Computer Science")
b. Create a separate file, letter.scm that contains
the definition of cr and the updated definition of
letter from the previous exercises.
c. In the definitions window, type the following
(load "sam.scm")
(load "letter.scm")
(display letter)
d. What do you expect to happen when you click Run?
e. Check your answer experimentally.
f. What ideas does this exercise suggest to you?
Exercise 11: Substrings
Consider the string "Department".
a. Write an expression to extract the string "Depart"
from "Department".
b. Write an expression to extract the string "part"
from "Department".
c. Write an expression to extract the string "art"
from "Department".
d. Write an expresssion to extract the string "ment"
from "Department".
e. Write an expression to extract the string "a"
from "Department".
f. Write an expression to extract the empty string
from "Department".
g. Write an expression to extract the string "Dent"
from "Department". Note that you may need to use
two calls to substring along with a call
to string-append.
h. Write an expression to extract the string "apartment"
from "Department". Once again, you may need multiple
calls.
Exercise 12: Deleting Letters
Write a procedure, (delete-char
str pos),
that creates a new string by deleting the character at position
pos from str.
For example,
> (delete-char "starlings" 8)
"starling"
> (delete-char "starling" 4)
"staring"
> (delete-char "staring" 2)
"string"
> (delete-char "string" 2)
"sting"
> (delete-char "sting" 1)
"sing"
> (delete-char "sing" 3)
"sin"
> (delete-char "sin" 0)
"in"
> (delete-char "in" 1)
"i"
> (delete-char "i" 0)
""
Example suggested by Jonathan Rebelsky.