Functional Problem Solving (CSC 151 2014F) : Labs
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Rubric] - [Calendar]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Readings]
Reference: [Setup] [VM] [Errors] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Davis (2013F)] [Rebelsky (2014S)] [Weinman (2014F)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)]
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.
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>?
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. 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.
c. No images needed in today's lab!
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 make-string
,
one a call to string
, and one a call to
list->string
.
Here are two opposing views about the relationship between
string-length
and string-ref
:
str
is, provided that it's not the
empty string, (string-ref str (string-length str))
will return
the last character in the string.”str
is, (string-ref str
(string-length str))
is an error.”Which, if either, of these views is correct? Why?
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 expresssion to extract the string "ment"
from "Department"
.
d. Write an expression to extract the string "a"
from "Department"
.
e. Write an expression to extract the empty string
from "Department"
.
f. 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
.
g. Write an expression to extract the string "apartment"
from "Department"
. Once again, you may need multiple
calls.
Write a procedure, (
,
that creates a new string by deleting the character at position
delete-char
str
pos
)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.
Write the following procedures:
(a)
(
, which converts each character in
string-sum
str
)str
to an integer and computes the sum of those
integers. For example,
>
(char->integer #\h)
104
>
(char->integer #\i)
105
>
(string-sum "hi")
209
(b)
(
, which produces string-palindrome?
str
)#t
if
str
reads the same
forwards and backwards; #f
otherwise. For example,
>
(string-palindrome? "ana")
#t
>
(string-palindrome? "anna")
#t
>
(string-palindrome? "hannah")
#t
>
(string-palindrome? "banana")
#f
Note: Many strategies are possible. Either list
recursion (after converting the string to a list of characters)
or numeric recursion (with string-ref
)
may help you solve these problems.
If you find that you have extra time, try any or all of the following problems.
Consider the following definitions:
(define letter (let ([cr (string #\newline)]) (lambda (recipient magazine article) (string-append "Dear " recipient ", " cr cr "Thank you for your submission to " magazine ". Unfortunately, we " cr "consider the subject of your article, " article ", inappropriate " cr "for our readership. In fact, it is probably inappropriate for any " cr "readership. Please do not contact us again, and do not bother " cr "other magazines with this inappropriate material or we will be " cr "forced to contact the appropriate authorities." cr cr "Regards," cr "Ed I. Tor" cr))))
a. What do you expect the output of the following expression to be?
(letter "Professor Schneider" "College Trustee News" "Using Grinnell's Endowment to Eliminate Tuition")
b. Check your answer experimentally.
c. What do you expect the output of the following expression to be?
(display (letter "Professor Schneider" "College Trustee News" "Using Grinnell's Endowment to Eliminate Tuition"))
d. Check your answer experimentally.
a. What changes are necessary to letter
so that name
of the article appears in quotation marks?
b. Check your answer experimentally.
A “shape poem” is a series of strings that make a shape, such as a triangle or square or diamond. One of the simplest shape poems can be constructed by repeatedly removing the last character of the string, as in the following image.
substring substrin substri substr subst subs sub su s
Write a procedure, (triangular-shape-poem
, that builds one of these shape poems from string
)string
.
>
(triangular-shape-poem "whatever")
whatever
whateve
whatev
whate
what
wha
wh
w
>
Note that when you want to return nothing from a procedure, you can use
the expression (void)
.
Extend your
string-palindrome?
predicate to work for proper names
and acronyms, e.g.,
>
(string-palindrome? "Anna")
#t
>
(string-palindrome? "APIPA")
#t
(APIPA stands for “Automatic Private IP Addressing”. The technology industry loves acronyms, and computer network experts love them even more.)
Write a procedure to replace one character in a string with another:
(
.
Some examples:
string-replace-char
str
old-char
new-char
)
>
(string-replace-char "banana" #\b #\f)
"fanana"
>
(string-replace-char "banana" #\a #\i)
"binini"
>
(string-replace-char "banana" #\x #\z)
"banana"
>
(string-replace-char "So long. Farewell. Auf Wiedersehen. Goodbye." #\. #\!)
"So long! Farewell! Auf Wiedersehen! Goodbye!"
Can you think of any useful extensions to this idea?