CSC 153 | Grinnell College | Spring, 2005 |
Computer Science Fundamentals | ||
Laboratory Exercise | ||
This laboratory exercise provides experience working with character data in Scheme.
Throughout this lab, you may want to refer to the Revised (5) report on the algorithmic language Scheme for information on various procedures involving characer data.
a A b B G ; ] 0 1 5 9
Use the comparison
(char<? #\a #\b)
to check whether the character a comes before the character b.
Use various comparison operations to determine whether an uppercase A comes before, comes after, or is equal to a lowercase a. Similarly, use these character comparison operations to confirm the relative orders of the characters from step 1.
Compare the case-insensitive versions of the comparison with various combinations of characters, including uppercase letters, lowercase letters, punctuation, and digits. For example, what are the results of the following?
(char-ci<? #\0 #\A) (char-ci<? #\0 #\a)
Try with several other pairs of characters. Conclude your experiments with the following:
(char<? #\] #\A) (char<? #\] #\a) (char-ci<? #\] #\A) (char-ci<? #\] #\a)
Describe and explain your observations.
Consider the following procedure that counts the number of times the letter A appears within a string of characters in either uppercase or lowercase form:
(define count-As (lambda (str) ;Pre-condition: str is a character string ;Post-condition: returns number of As in str (count-As-kernel (string->list str)) ) ) (define count-As-kernel (lambda (ls) ;Pre-condition: str is a list of characters ;Post-condition: returns number of As in ls, ignoring case differences (cond ((null? ls) 0) ((char-ci=? #\a (car ls)) (+ 1 (count-As-kernel (cdr ls)))) (else (count-As-kernel (cdr ls))) ) ) )
Check that count-As works by testing it with several strings,
including
(count-As "This is a string with 2 A's").
Define a procedure vowel? which has a single parameter and which returns true if the parameter's value is a vowel and false otherwise.
The following procedure illustrates some code that has been turned in the past few semesters by students. While the code works, it may be simplified significantly. Write equivalent code which is as simple as possible:
(define compare? (lambda (ch1 ch2) ;Pre-condition: ch1 and ch2 are characters ;Post-condition: returns true if character ch1 comes before character ch2 (if (< (char->integer ch1) (char->integer ch2)) #t #f ) ) )
Modify count-As to obtain a procedure count-vowels which determines the number of vowels in a string.
Modify count-As and count-As-kernel, so that count-As-kernel is tail recursive.
Refer to the Revised Report (5) to identify other character procedures that are built into Scheme. Then, modify count-As to count the number of uppercase letters that appear in a string.
(Optional, and a bit more of a challenge) Modify count-As to obtain a procedure count-punc which determines the number of punctuation marks within a string. [For this problem, you first may need to decide just what should be considered as a punctuation mark.]
This document is available on the World Wide Web as
http://www.walker.cs.grinnell.edu/courses/153.sp05/labs/lab-characters.shtml
created February 26, 1997 last revised February 1, 2005 |
![]() ![]() |
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |