Computer Science Fundamentals (CS153 2004S)

Characters and Strings in Scheme

Summary: In these exercises, you will explore a number of the s tandard Scheme procedures for handling characters and strings. You will also ex plore an application of these procedures for marking up text.

Contents:

Related Pages:

Useful Procedures and Notation:

Exercises

Exercise 0: Preparation

Start DrScheme.

Exercise 1: Collating Sequences

As you may recall, Scheme uses a collating sequence for the letters, assigning a sequence number to each letter. DrScheme uses 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: A Control Predicate

In ASCII, the collating-sequence numbers of the control characters are 0 through 31 and 127. Define a predicate char-control? that returns #t if its argument is a control character, #f otherwise.

Note: The char-control? predicate takes a character, not an integer, as a parameter. (It need not verify the type of the parameter, but it should work with characters.)

Exercise 3: String Basics

a. Is the symbol 'hyperbola a string?

b. Is the character #\A a string?

c. Does the empty string, "", count as a string?

Exercise 4: Creating Questions

Suggest 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:

Which, if either, of these views is correct? Why?

Exericse 6: Counting Vowels

Write a Scheme procedure, (tally-vowels str), that determines how many characters in str are vowels. (For this purpose, you should count only the ten characters #\a, #\e, #\i, #\o, #\u, #\A, #\E, #\I, #\O, and #\U as vowels.)

You may not convert str to a list before counting. (That is, you'll need to use string-ref to look at the individual characters.)

Exercise 7: From List of Symbols to String

Write a procedure, (symbol-list->string los) that converts a list of symbols to a string for that list of symbols. You may find the symbol->string procedure helpful.

For example,

> (symbol-list->string (list 'a 'b 'c))
"(a b c)"
> (symbol-list->string (list))
"()"
> (symbol-list->string (list 'alpha 'bravo 'charlie))
"(alpha bravo charlie)"

Hint: Convert the elements of the list to a string using a kernel procedure and then add the parens.

 

History

Tuesday, 3 October 2000 [Samuel A. Rebelsky]

Wednesday, 7 February 2001 [Samuel A. Rebelsky]

Sunday, 18 February 2001 [Samuel A. Rebelsky]

Monday, 16 September 2002 [Samuel A. Rebelsky]

Friday, 7 February 2003 [Samuel A. Rebelsky]

Monday, 10 February 2003 [Samuel A. Rebelsky]

Monday, 10 February 2003 [Samuel A. Rebelsky]

Sunday, 8 February 2004 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Fri May 7 09:44:24 2004.
The source to the document was last modified on Mon Feb 9 10:36:45 2004.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2004S/Labs/strings.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu