Fundamentals of Computer Science I (CS151.01 2006F)
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]

[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Homework]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151.02 2006F (Davis)]
[CSCS151 2005S (Stone)]
[CSC151 2003F (Rebelsky)]
[CSC153 2004S (Rebelsky)]
Summary: In this laboratory, you will explore the basic operations for working with lists and symbolic values. You may want to refer to the reading on symbols and the reading on lists as you work on this lab.
Important Procedures:
append
,
car
,
cdr
,
cons
,
length
,
list
,
listref
, and
reverse
.
Contents:
cons
Operations
list
Procedure
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
Operationsa. 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.
a. Determine the result of the expression (cons 'a 'b)
.
b. Determine the result of consing 'd
to the front of the
value you created in step a.
Neither of these values are lists! Make sure that you are able to distinguish them from lists when looking at results.
list
Procedure
Call the procedure list
, supplying the numerals
17
and 43
as arguments. Describe the value
returned by the procedure.
a. How would you call the list
procedure to create a list
containing the symbols alpha
, beta
, and
gamma
, in that order?
b. Verify your answer by entering the code in DrScheme.
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.
cdr
a. What is the cdr
of a oneelement list?
b. Verify your answer by experimentation.
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.
a. Does it make sense to apply car
and cdr
to
values other than lists? Why or why not?
b. 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)))
. You may
use list
, cons
, or a combination of
the two.
Use Scheme to give the name Greekletters
to the list
constructed by the expression
(list 'alpha 'beta (list 'gamma1 'gamma2) 'delta)
Then use the length
procedure to
confirm that it has four elements.
Determine the length of the empty list.
a. Create a list of length 5. I don't care what appears in the list, provided that it is in good taste.
b. Check your answer by having Scheme compute the length of that list.
a. Create the list (a (b c) (d (e)))
and name it letters
.
b. What do you think the length of letters
should be?
c. Experimentally determine the length of letters
.
d. Explain the result.
Use Scheme to compute the reversal of the list whose elements
are the symbols senior
, thirdyear
,
secondyear
, and freshling
, in that order.
a. If a list has another list as one of its elements (as in the case of
letters
above), should reverse
reverse that
inner list as well as the outer one?
b. Find out by experiment what Scheme does.
a. Create a list named symbols
with the elements
alpha
and beta
as its elements.
b. Create a list named numbers
with the numbers 1, 2,
and 3 as its elements.
c. Use Scheme to find the result of joining together (with
append
) symbols
and numbers
.
d. How many elements does the resulting list have?
a. Invoke the procedure list
, applying it to symbols
and numbers
from the previous exercise.
b. How many elements does the resulting list have?
c. The answer to this question is different from the answer to the question at the end of the previous exercise. Why?
a. Write a call to the procedure cons
, applying it to
symbols
and numbers
.
b. How many elements does the resulting list have?
c. Why is the answer to this question different from the answers to the questions at the end of the previous two exercises?
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.
Quit DrScheme and log out of the workstation.
If you have extra time,
(a (b c) (d (e)))
using cons
rather than quote or list
.
(a (b (c (d (e ())))))
using cons
.
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.
2 Septemer 1997 [John David Stone]
31 March 2000 [John David Stone]
http://www.cs.grinnell.edu/~stone/courses/scheme/spring2000/symbolsandlists.xhtml
.
Friday, 1 September 2000 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2000F/Labs/lists.html
.
Sunday, 28 January 2001 [Samuel A. Rebelsky]
Monday, 29 January 2001 [Samuel A. Rebelsky]
list
problem to handle
inclass questions.
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2001S/Labs/lists.html
.
Friday, 6 September 2002 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Labs/lists.html
.
Wednesday, 22 January 2003 [Samuel A. Rebelsky]
Tuesday, 28 January 2003 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2003S/Labs/lists.html
.
Wednesday, 30 August 2006 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2006F/Labs/lists.html
.
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]

[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Homework]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151.02 2006F (Davis)]
[CSCS151 2005S (Stone)]
[CSC151 2003F (Rebelsky)]
[CSC153 2004S (Rebelsky)]
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 Thu Nov 30 21:43:03 2006.
The source to the document was last modified on Wed Aug 30 11:03:34 2006.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2006F/Labs/lists.html
.
You may wish to validate this document's HTML ; ;
Samuel A. Rebelsky, rebelsky@grinnell.eduhttp://creativecommons.org/licenses/bync/2.5/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.