Letter Case Conversion

Kurt Nørmark ©    normark@cs.auc.dk    Department of Computer Science, Aalborg University, Denmark    

Abstract. This is the solution to the letter case conversion exercise.

 

   intr-section
1  Introduction
As for other exercises, we point a solutions in a LAML library file, here general in lib4/general.scm
1.1  capitalize-string
1.2  upcase-a-string
1.3  down-case-string
 

Introduction  upcase-a-string cap-string
1.1  capitalize-string
(Perhaps we should use the function on the title...)

In the general library we both support destructive procedures and pure functions for letter conversion puposes. In this story, we are only concerned with the pure functions. Somehow I have regretted that I ever made the mutator procedures...

As a consequence I will here show good functional solutions, aided by some primitive functions in the general library.

First the function capitalize-a-string. If the input string is empty we decide that the result is also the empty string. As such, we go for a weak pre condition.

If the string is non-empty we capitalize the first character by capitalize-char from the general library. The as-string'ed first capitalized char is then string-appended to the suffix substring of str. That is it! 

 

Introduction capitalize-string down-case-string up-string
1.2  upcase-a-string
The function
upcase-a-string makes heavy use of the two native Scheme functions list->string and string->list. string->list explodes a string to a list of its characters. list->string does the opposite. Given a list of characters, we map capitalize-char over this list to get the all caps list. list->string assembles the resulting string. That was easy! 

 

Introduction upcase-a-string  down-string
1.3  down-case-string
The function
downcase-a-string is similar. It applies decapitalize-char instead of capitalize-char. So that was trivial!