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.
"string" (string constants).
#\a (lowercase a) ...
#\z (lowercase z);
#\A (uppercase A) ...
#\0 (zero) ...
#\newline (newline); and
#\? (question mark).
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.
d. No images needed in today's lab!
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?
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
d. Determine whether our implementation of Scheme indicates that
#\A are the same letter. (It should
e. Find an equality predicate that returns
#t when given
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?
Develop three ways of constructing the string
one using a call to
one a call to
string, and one a call to
Here are two opposing views about the relationship between
stris, provided that it's not the empty string,
(string-ref str (string-length str))will return the last character in the string.”
(string-ref str (string-length str))is an error.”
Which, if either, of these views is correct? Why?
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
"Ms. Davis" and
"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.)
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
(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?
adjective in such a way that
tunes can be successfully defined.
c. Write a procedure that behaves like this definition.
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
b. Check that the definition of
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
procedure, as in
(display letter). Try doing so.
a. What changes are necessary to
letter so that name
of the article appears in quotation marks?
b. Check your answer experimentally.
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?
e. Check your answer experimentally.
f. What ideas does this exercise suggest to you?
Consider the string
a. Write an expression to extract the string
b. Write an expression to extract the string
c. Write an expression to extract the string
d. Write an expresssion to extract the string
e. Write an expression to extract the string
f. Write an expression to extract the empty string
g. Write an expression to extract the string
"Department". Note that you may need to use
two calls to
substring along with a call
h. Write an expression to extract the string
"Department". Once again, you may need multiple
Write a procedure,
that creates a new string by deleting the character at position
(delete-char "starlings" 8)
(delete-char "starling" 4)
(delete-char "staring" 2)
(delete-char "string" 2)
(delete-char "sting" 1)
(delete-char "sing" 3)
(delete-char "sin" 0)
(delete-char "in" 1)
(delete-char "i" 0)
Example suggested by Jonathan Rebelsky.
You may recall that the names of the ten basic circle brushes follow
a regular pattern: the word “Circle” followed by a
space, an open parenthesis, an odd number, and a close parenthesis.
For example, the first circular brush is
and the sixth circular brush is
Write a procedure,
(circular-brush n), that computes
the name of the
nth circular brush. This procedure
should throw an error if
n is invalid. Please use the
string procedures, such as
string-append, in defining your procedure.
(circular-brush "One")circular-brush requires an integer between 1 and 10
(circular-brush -5)circular-brush requires an integer between 1 and 10
(circular brush 1.5)circular-brush requires an integer between 1 and 10
(circular brush 1925)circular-brush requires an integer between 1 and 10
Write another procedure to generate a form letter of your choice. Possibilities include acceptance letters to Grinnell, rejection letters, and recommendation letters.
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
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.