Workshop on Scheme

Exercises: Tuesday, July 18

Reading: Scheme and the art of programming, sections 5.1 and 5.2 and chapter 6; Revised(4) report on the algorithmic language Scheme, sections 3, 4.1, 4.2.1 through 4.2.4, 5, 6.1, 6.2, 6.4, 6.6, 6.7, and 6.10.


From Scheme and the art of programming: exercises 5.1, 5.5, 5.7, 6.1, 6.11.


Write the definition of fib using a named let-expression to control the iteration.


Define a procedure fixed-point that takes three arguments -- a real number num, a positive integer field-width, and a non-negative integer fraction-length -- and returns a string that contains a fixed-point representation of that number. If the fraction-length operand is 0, the representation should be the ordinary decimal numeral for the integer nearest to the num operand; if the fraction-length operand is positive, the representation should contain a decimal point, with the integer part of the value of num to its left and to its right exactly the number of decimal places specified by the value of fraction-length, correctly rounded. If the value of num is negative, the representation should begin with a minus sign. Finally, if the total number of characters in the representation is less than the value of field-width, the representation should be preceded by enough spaces to pad it out to the total string length specified by field-width.

The idea is to have fixed-point return the sequence of characters that a Pascal program would print in executing the statement write (num : fieldwidth : fractionlength).


Define a procedure translate that takes three string arguments, template, outs, and ins, subject to the precondition that (= (string-length outs) (string-length ins)), and returns the string that results from applying a character mapping to the value of template in which each character that also appears in the value of outs is replaced by the corresponding character from ins. (Characters of template that do not appear in outs are copied into the result without change.) If the same character appears more than once in outs, the rightmost correspondence with ins applies.

Test your procedure on the following cases:

(translate "mad hatter" "a" "+") => "m+d h+tter"
(translate "mad hatter" "aeiou" "12345") => "m1d h1tt2r"
(translate "mad hatter" "aeiou" "+++++") => "m+d h+tt+r"
(translate "654321" "123456" "diaper") => "repaid"
(translate "hr:mi:se" "hrmise" "035642") => "03:56:42"
(translate "ab" "abbbbbb" "Georgia") => "Ga"
(translate "123321" "123" "red") => "redder"


This document is available on the World Wide Web as

http://www.math.grin.edu/~stone/events/scheme-workshop/Tuesday-exercises.html


created July 15, 1995
last revised July 16, 1995

John David Stone (stone@math.grin.edu)