[plt-scheme] Scheme Programming Question (Making an error message)
Can anyone do this? This is my homework, and I am really having
trouble with it.
Can any smart scientist do this?
Question Details:
Exercise 13. For Assignment 4, you defined the procedure reverse-list.
A call to your version of reverse-list probably resulted in the
following sort of error when passed an invalid argument.
-------------------------------
(define combine-lists
(lambda (a b)
(if (null? a) b (combine-lists (cdr a) (cons (car a) b)))))
(define reverse-list
(lambda (ls)
(combine-lists ls '())))
---------------------------------
> (reverse-list 3)
Error in cdr: 3 is not a pair.
> (reverse-list '(a b . c))
Error in cdr: c is not a pair.
Define reverse-list using an internally defined help procedure. Have
the help procedure signal an error if the incoming argument is not a
pair or empty list. Your solution should make only one pass through
the list. In particular, you should not use list?, proper-list?, or
the equivalent to first check if the list is proper, since this would
count as an additional pass through the list. (Of course, you may use
the built-in pair? predicate.) (Naturally, you may not use the built-
in reverse procedure since the point of the exercise is to implement
that procedure.)
Identify the error as coming from reverse-list, not from the helper,
and show the original input to reverse-list in the error message, even
if the error occurs some iterations into the loop.
> (reverse-list 3)
Error in reverse-list: 3 is not a proper list.
> (reverse-list '(a . b))
Error in reverse-list: (a . b) is not a proper list.
> (reverse-list '(a b . c))
Error in reverse-list: (a b . c) is not a proper list.
> (reverse-list '(should it work? this does))
(does this work? it should)
Hint: The text of this exercise tells exactly how to detect the error.