[plt-scheme] Scheme Programming Question (Making an error message)

From: Jos Koot (jos.koot at telefonica.net)
Date: Sun Oct 5 06:49:59 EDT 2008

In procedure <combine-lists> two cases are discerned (either <a> is empty or 
it is something else) You should discern more cases. How do you check an 
object to be a list without using predicate <list?>? You may want to first 
design procedure <list?>, before incorporating its body in <combine-lists>.

Jos

----- Original Message ----- 
From: "Geneve" <kyung8653 at gmail.com>
To: <plt-scheme at list.cs.brown.edu>
Sent: Saturday, October 04, 2008 7:11 PM
Subject: [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.
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> 



Posted on the users mailing list.