|CSC 153||Grinnell College||Spring, 2005|
|Computer Science Fundamentals|
This laboratory exercise considers output procedures to aid in the tracing of a program. This lab also discusses interactive Scheme programming, using read, display and write statements. Finally, the lab mentions the use of sentinel values to halt the reading of data from the keyboard.
Consider the following two versions of a procedure to add 2 to each element in a list:
(define simple-add2 (lambda (L) ;Pre-condition: L is a list of numbers ;Post-condition: returns a new list where 2 is added to each value in L (if (null? L) '() (cons (+ 2 (car L)) (simple-add2 (cdr L)))) ) ) (define simple-add2-trace (lambda (L) ;Pre-condition: L is a list of numbers ;Post-condition: returns a new list where 2 is added to each value in L (display L) (newline) ;;NEW: print out current list on separate line (if (null? L) '() (cons (+ 2 (car L)) (simple-add2-trace (cdr L)))) ) )
Check that procedure simple-add2 works correctly, by running it
with the command
Run the revised proceduresimple-add2-trace with the same data you
used in styp 1, by typing
(simple-add2-trace '(2 6 -3 0 4.689))
Write a few sentences to explain what appears at your screen:
Experiment further with display and newline.
Now consider this further-revised version of simple-add2-trace:
(define simple-add2-trace (lambda (L) ;Pre-condition: L is a list of numbers ;Post-condition: returns a new list where 2 is added to each value in L (display L) (if (null? L) (begin (display " then clause") (newline) '() ) (begin (display " else clause") (newline) (cons (+ 2 (car L)) (simple-add2-trace (cdr L))) ) ) ) )
(define simple-add2-trace (lambda (L) ;Pre-condition: L is a list of numbers ;Post-condition: returns a new list where 2 is added to each value in L (display L) (if (null? L) (begin '() (display " then clause") (newline) ) (begin (cons (+ 2 (car L)) (simple-add2-trace (cdr L))) (display " else clause") (newline) ) ) ) )
Consider the following procedure that reads 3 scores from the keyboard and averages them:
(define average-3 (lambda () ;Pre-condition: None ;Post-condition: returns the average of 3 numbers read from the keyboard (display "Enter three numbers: ") (display (/ (+ (read) (read) (read)) 3.0)) (display " is the average of the three numbers") (newline) ) )
What happens if you enter three fractions or some negative numbers when you run this procedure?
What happens if you enter a string instead of a number when you run this procedure?
Suppose you wanted to average any number of values; you might ask the computer to continue reading numbers until you entered a 0. In this context, you could pass the current sum and the number of values read to the kernel procedure. The base case would be identified by checking if the current value read was zero.
Write an average procedure which reads successive numbers until 0 is entered and computes and prints the average of these numbers. Be sure you do not include 0 in your computation of the average.
Note: In this problem, the number 0 is called a sentinel. Generally, a sentinel is a value read that tells the computer something special about how processing should proceed.
Consider the following procedures to create a table of square roots:
(define sqrt-table (lambda (n) (display "Number Square Root") (newline) (sqrt-table-helper n) ) ) (define sqrt-table-helper (lambda (n) (if (>= n 1) (begin (sqrt-table-helper (- n 1)) (display n) (display (sqrt n)) (newline)) ) ) )
Run this procedure to obtain a table of the square roots of the integers 1, 2, 3, 4, and 5.
Within your Scheme environment, load the customized I/O package ~walker/151s/labs/write-formatted.ss with the statement:
This package, written by John David Stone, contains procedures write-int and write-real for formatting integer and real numbers, respectively. The write-int procedure asks for the number to be printed and the width of the field for printing. In our example, we want 4 spaces for our integer n (3 blank spaces plus the digit). Thus, we write
(write-int n 4)Similarly, for real numbers, we supply write-real with the number to be printed, the total width for printing the number, and the desired number of decimal places. In this example, the number takes 14 characters overall, with 5 decimal place accuracy:
(write-real (sqrt n) 14 5)
display statements in
sqrt-table-helper by the corresponding
and write-real statements, and check that the new formatting works
write-int with various integers and widths to check that
it runs correctly. What happens if
write-int is given a width
that is narrower than required by the number to be printed (e.g., 2 spaces
allocated for the printing of 456)?
Run write-real with a several real numbers, widths, and fractional-lengths to check that it also runs correctly.
This document is available on the World Wide Web as
created March 5, 1997
last revised February 2, 2005
|For more information, please contact Henry M. Walker at email@example.com.|