CSC 151-02, Fall 2006 : Schedule : Lab 21
behind the sceneswhen Scheme deals with lists and other pair-based structures.
Contents:
Make sure you have some blank pieces of paper (lined is okay) and something with which to write.
Draw box-and-pointer diagrams for each of the following lists:
((x) y z)
(x (y z))
((a) b (c ()))
Enter each of the following expressions into Scheme. In each case, explain why Scheme does or does not use the dot notation when displaying the value.
(cons 'a "spam")
(cons 'a null)
(cons 'a "null")
(cons 'a "()")
(cons null 'a)
(cons null (cons null null))
Draw a box-and-pointer representation of the value of the last two expressions in the previous exercise.
What do you think that pair? will return for each of the
following? How about list?. Attempt to confirm each answer
experimentally and explain any that you found particularly tricky.
(cons 'a 'b)
(cons 'a (cons 'b 'c))
(cons 'a null)
null
(list 'a 'b 'c)
(list 'a)
(list)
You may recall that I told you that many kinds of data are defined recursively. For example, a list is either (1) null or (2) cons of anything and a list.
Using that recursive
definition of lists, write a procedure,
(listp? val), that determines whether
or not val is a list.
You may not use list? in your definition of
listp?.
If you were able to complete the primary exercises with time to spare, you might want to consider the following problems.
Write a procedure, (last pairthing) that finds the last
value in a list-like pairs structure. If the pair structure is actually
a list, return the last element of the list. Otherwise, follow the cdrs
until you find the last pair, and return the cdr of that pair.
In solving this problem you should only step through the list once.
listp?
Write listp? without using if or cond.
Janet Davis (davisjan@cs.grinnell.e
Created October 8, 2006 based on http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2006F/Labs/pairs.html