This lab is also available in PDF.
Summary: In this laboratory, you will experiment with the use and application of some of Scheme's basic input and output procedures.
a. Make sure that you understand what
are supposed to do. You may find
the reading on input and
the reading on output
b. Ensure that you understand the sample code from the reading on input.
c. Start DrScheme.
For each of part of this exercise, you should use something like
(define val (read))
to read in a value.
> (define val (read))
45> val 45
read to obtain a number. That is, call
read as above and enter a number.
read to obtain a string. (You'll need to type in a string.)
read to obtain a character. (You'll need to type in a character in traditional form.)
read to obtain a list of numbers. (You should not write the word
read to obtain a symbol. Verify that the value you obtained is a symbol using the
read to obtain a list that contains a number, a string, and a symbol.
g. What happens when you use
read to obtain a list and
you hit <Enter> in the middle of the list?
Consider the following sequence of Scheme commands:
(display "Please enter a value and I will square it: ") (define val (read)) (define val-squared (* val val)) (display (string-append "The value of " (number->string val) " squared is " (number->string val-squared))) (newline)
a. What do you expect the code to do?
b. Verify your answer via experimentation.
Rewrite the code from the previous exercise to use
let* (or both) rather than
a. Save the above code in a file (e.g.,
square.scm in your
b. Open a terminal window.
c. In that terminal window, type
/usr/bin/mzscheme -r file.scm
Where file.scm is the name you chose in part a.
d. Reflect on what happened in step c. Did you need to type any Scheme? Could someone else use step c without understanding the underlying Scheme?
e. Create a file called
square that contains the
#!/bin/bash /usr/bin/mzscheme -r file.scm
where file.scm is the name you chose in part a.
Although the preceding is not Scheme code, you can still enter it in DrScheme and save it into a file.
f. In the terminal window, type
chmod 755 square
g. In the terminal window, type
h. Reflect on what just happened.
a. What happens in your square program if someone enters something other than a number?
b. Update your program so that it prints a friendly error message
display) and then asks again if someone enters
something other than a number.
a. Write a Scheme program that reads in the three coefficients of a quadratic equation (the a, b, and c in ax2 + bx + c) and prints out the roots of the equation. You should model this program on the previous exercises. In case you've forgotten, the roots of the quadratic equation are
(-b +/- sqrt(b2 - 4ac)) / 2a
b. Save the program in a file and execute it from the command line.
c. Reflect on what happened. Did you need to type any Scheme? Could someone else use step b without understanding the underlying Scheme?
Here is a simple story procedure from the reading on program output.
(define story1 (lambda (lst) (if (null? lst) (begin (display "Daniel likes other things, too.") (newline)) (begin (display "Daniel likes ") (display (car lst)) (display ".") (newline) (story1 (cdr lst))))))
a. Make sure that it can tell a story.
b. Write your own variant of this procedure.
The reading on input contains a program that repeatedly asks for a value and computes its square root.
a. Verify that it works correctly.
b. Create a shell script (as above) that lets us compute square roots within a terminal window.
Rewrite the story telling procedure from exercise 7 so that it prompts for the words in the story (that is, the things that Daniel likes). Try to ensure that the story does not get told until after all of the words are entered.
I usually create these pages
on the fly, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Thu Sep 13 20:54:20 2007.
The source to the document was last modified on Tue Feb 27 10:25:21 2007.
This document may be found at
You may wish to validate this document's HTML ; ;Samuel A. Rebelsky, firstname.lastname@example.org
http://creativecommons.org/licenses/by-nc/2.5/or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.