Q BgQuestion:

      
`
Novice
Karma Points: 35
Respect (83%):
posted by  ` on 10/4/2008 1:08:21 AM  |  status: Live  |  Earned Karma: Label

Scheme Programming Question (Making an error message)

Course Textbook Chapter Problem Needs by
N/A N/A N/A N/A N/A
Question Details:
xercise 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.
Bonus Point Alert! Earn +7 additional karma points for helping this gold member.

AAnswers:

Answer Question Ask for clarification

No one has answered this question yet.

Be the first to answer. Earn up to 10 karma points.

Answer Question Ask for clarificarion

Join Cramster's Community

Cramster.com brings together students, educators and subject enthusiasts in an online study community. With around-the-clock expert help and a community of over 100,000 knowledgeable members, you can find the help you need, whenever you need it. Join for free today » How Cramster is different from tutoring »