Computer Science Fundamentals (CS153 2003S)
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]

[EC]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Lab Writeups]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Experiments in Java]
[Scheme Reference]
[Scheme Report]
[CS153 2002S (Walker)]
[CS151 2003S (Rebelsky)]
[CS152 2000F (Rebelsky)]
[SamR]
In this lab, you will explore the basic operations for working with three core types in Scheme: lists, symbolic values, and numbers. You may want to refer to the reading on lists, the reading on symbols, and the reading on numbers as you work on this lab.
List constructors used in this lab:
cons
and
list
.
List extractors used in this lab:
car
,
cdr
,
length
, and
listref
.
Other list procedures used in this lab:
append
and
reverse
.
Number predicates used in this lab:
complex?
,
exact?
,
inexact?
,
number?
,
rational?
, and
real?
,
Other number procedures used in this lab:
denominator
,and
numerator
.
cons
es
list
procedure
car
and cdr
Start DrScheme.
a. Call the cons
procedure to create a list that has the number 1 as its first and only
element. The result of your call should be
(1)
.
b. Call the cons
procedure to create a list that has the symbols a
and
b
as its two elements. The result of your call should be
(a b)
.
cons
esa. Figure out (without using DrScheme) the result of the following expression.
(cons 'alpha (cons 'beta (cons 'gamma (cons 'delta null))))
b. Check your answer by asking DrScheme to evaluate this expression.
list
procedure
Call the procedure list
, supplying the numerals
17
and 43
as arguments. Describe the value
returned by the procedure.
How would you invoke the list
procedure to create an
empty list?
Determine by experiment whether it is possible to create a list in which the same element occurs more than once.
car
and cdr
a. What is the cdr
of a
oneelement list?
b. Verify your answer by experimentation.
c. It makes no sense to apply the car
and cdr
procedures to an empty list,
because there's no way to split off the first element
of a list
that has no elements. What happens if you try it anyway? Find out by
having DrScheme evaluate a deliberately incorrect procedure call.
d. Does it make sense to apply car
and cdr
to
values other than lists? Why or why not?
e. Determine what happens if you apply these procedures to symbolic values and numeric values.
a. Create the list (e)
.
b. Create the list (d (e))
.
c. Create the list (b c)
.
d. Create the list (a (b c) (d (e)))
using list
.
e. Create the list (a (b c) (d (e)))
using cons
.
Determine the length of the empty list.
a. Create the list (a (b c) (d (e)))
b. What do you think the length of this list should be?
c. Experimentally determine the length of this list.
d. Explain the result.
Use Scheme to compute the reversal of the list whose elements are the
symbols senior
, junior
, sophomore
,
and freshling
, in that order.
a. If a list has another list as one of its elements, should
reverse
reverse
that inner list as well as the outer one?
b. Find out by experiment what Scheme does.
a. Use Scheme to find the result of stringing together (with append
) a list with the
symbols alpha
and beta
as its elements and a
list with the numbers 1, 2, and 3 as its elements.
b. How many elements does the resulting list have?
c. Invoke the procedure list
, applying it to the two
lists that you strung together in the previous exercise: a list with the
symbols alpha
and beta
as its elements and a
list with the numbers 1, 2, and 3 as its elements.
d. How many elements does the resulting list have?
e. The answer to this question is different from the answer to b. Why?
f. Write a call to the procedure cons
, applying it to our
favorite two lists: a list with the symbols alpha
and
beta
as its elements and a list with the numbers 1, 2, and
3 as its elements.
g. How many elements does the resulting list have?
h. Why is the answer to this question different from the answers to b and d?
If you are puzzled by this exercise, you may want to read the notes on the exercise.
Write a call to the listref
procedure that
will extract the fourth element of the list
(38 72 apple 1/3 sample)
That is, you should extract the number 1/3.
Have DrScheme confirm that 3/4 is a rational number but not an integer and that the square root of 1 is a complex number but not a real number.
Confirm that the value DrScheme computes for (sqrt 2)
is an
inexact real that is also rational.
As you've just seen, some kinds of numbers are subsets of other kinds of numbers. Determine the relationships between the various kinds of numbers.
a. Have DrScheme find the square of the square root of 2 and subtract 2 from the result.
b. Ideally, the difference should be 0; why isn't it?
c. How big is the difference?
d. Will you have the same problem if you start with 4? Why or why not?
Scheme provides a number of numerical procedues that can produce integer results.
Here are some others. For each, try to figure (by experimentation, by discussing results with other students, and, eventually, by reading documentation) out how many parameters each procedure can take and what the procedure does. You may not be able to figure all of them out. Make sure to try a variety of values for each procedure, including positive and negative, integer and real.
a. quotient
b. remainder
c. modulo
d. max
e. min
f. numerator
g. denominator
h. gcd
i. lcm
j. floor
k. ceiling
l. truncate
m. round
Since you've found that DrScheme seems to represent every real number as a rational, it might be worth finding a way to see what that rational number is. Determine the numerator and denominator of the rational representation of the square root of 2.
For small numbers, the exact>inexact
procedure produces about six digits after the decimal point. Figure out
how to get just two digits after the decimal point. You may need to
use multiplication, division, and some of the last procedures from the
previous exercise.
You need not implement your algorithm; simply come up with one you think will work.
Quit DrScheme and log out of the workstation.
The append
procedure
joins together the elements of a list to make a new list. Hence, when
you append two lists together, the total number of elements in the new
list is the sum of the number of elements in the lists.
The list
procedure
creates a new list whose elements are the parameters to list
. Hence, if list
takes two parameters, the
length of the result is always two, regardless of what those parameters
are.
The cons
procedure
builds a new list by placing its first parameter at the start of its
second parameter (which is a list). Hence, the length of the result is
one more than the length of the second parameter.
When you get stuck on this problem, it's probably worth skimming through DrScheme's Help Desk. The numeric operations are documented in section 6.2.5 of the Revised(5) Report on the Algorithmic Language Scheme. (I have not yet put all of these procedures in the Glimmer Scheme Reference.)
Wednesday, 22 January 2003 [Samuel A. Rebelsky]
Monday, 27 January 2003 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Labs/types.html
.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]

[EC]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Lab Writeups]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Experiments in Java]
[Scheme Reference]
[Scheme Report]
[CS153 2002S (Walker)]
[CS151 2003S (Rebelsky)]
[CS152 2000F (Rebelsky)]
[SamR]
Disclaimer:
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 Tue May 6 09:20:05 2003.
The source to the document was last modified on Mon Jan 27 08:10:12 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Labs/types.html
.
You may wish to validate this document's HTML ; ; Check with Bobby
Samuel A. Rebelsky, rebelsky@grinnell.edu