From: ····@hotmail.com
Subject: Help with error handling
Date: 
Message-ID: <6tpe0u$3fc$1@nnrp1.dejanews.com>
Hi,

I'm new to LISP.  I'm trying to write a recursive function which will accept
2 kinds of lists, integers <eg. '(-5 6 2 0)> and characters <eg. '(a c b z)>,
and sort them.	If the elements of the list argument if a mix of both type,
the function should return a NIL.

Right now my function will bomb out with an invalid argument error message
when it uses > to compare integer and character or when it uses string> to
compare character and integer, but I don't want to see the error message,
just a NIL.  I tried to use the ignore-errors macro... it worked fine with
'(3 2 1 Z), but when I have '(z y x 1) it won't return NIL but return (y z).

So my real question is, is there anyway I could exit OUT of the recursive
function to the toplevel and return NIL whenever any error occurs deep inside
the recursive loop.

Thanks in advance!

NC

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum

From: David B. Lamkins
Subject: Re: Help with error handling
Date: 
Message-ID: <dlamkins-1609981804170001@192.168.0.1>
In article <············@nnrp1.dejanews.com>, ····@hotmail.com wrote:

>Hi,
>
>I'm new to LISP.  I'm trying to write a recursive function which will accept
>2 kinds of lists, integers <eg. '(-5 6 2 0)> and characters <eg. '(a c b z)>,
>and sort them.  If the elements of the list argument if a mix of both type,
>the function should return a NIL.
>
>Right now my function will bomb out with an invalid argument error message
>when it uses > to compare integer and character or when it uses string> to
>compare character and integer, but I don't want to see the error message,
>just a NIL.  I tried to use the ignore-errors macro... it worked fine with
>'(3 2 1 Z), but when I have '(z y x 1) it won't return NIL but return (y z).
>
>So my real question is, is there anyway I could exit OUT of the recursive
>function to the toplevel and return NIL whenever any error occurs deep inside
>the recursive loop.

Yes, look at catch and throw.

-- 
David B. Lamkins <http://www.teleport.com/~dlamkins/>
From: Steve Gonedes
Subject: Re: Help with error handling
Date: 
Message-ID: <m2ww73pdkf.fsf@KludgeUnix.com>
····@hotmail.com writes:


< Hi,
< 
< I'm new to LISP.  I'm trying to write a recursive function which will accept
< 2 kinds of lists, integers <eg. '(-5 6 2 0)> and characters <eg. '(a c b z)>,
< and sort them.	If the elements of the list argument if a mix of both type,
< the function should return a NIL.
< 
< Right now my function will bomb out with an invalid argument error message
< when it uses > to compare integer and character or when it uses string> to
< compare character and integer, but I don't want to see the error message,
< just a NIL.  I tried to use the ignore-errors macro... it worked fine with
< '(3 2 1 Z), but when I have '(z y x 1) it won't return NIL but return (y z).
< 
< So my real question is, is there anyway I could exit OUT of the recursive
< function to the toplevel and return NIL whenever any error occurs deep inside
< the recursive loop.


Are you familiar with the function `every'? This might help to prevent such
situations.

(defun my-sort (list)
  (cond ((every #'characterp list)
         (my-sort-with #'some-function-to-sort-stuff list))
        ((every #'numberp list)
         (my-sort-with #'< list))
        (t nil)))

You can use handler-case to handle a specific condition. You could
have it return nil for your sorting function if a type-condition
arises.

(handler-case (+ 1 #\e)
  (type-error (ignored)
    nil))

=> nil

Hope this helps some.
From: Rainer Joswig
Subject: Re: Help with error handling
Date: 
Message-ID: <joswig-1709982025090001@194.163.195.67>
In article <··············@KludgeUnix.com>, Steve Gonedes
<········@worldnet.att.net> wrote:

> < So my real question is, is there anyway I could exit OUT of the recursive
> < function to the toplevel and return NIL whenever any error occurs deep
inside
> < the recursive loop.

RETURN-FROM, Catch + Throw, error + handler, ...

> 
> 
> Are you familiar with the function `every'? This might help to prevent such
> situations.
> 
> (defun my-sort (list)
>   (cond ((every #'characterp list)
>          (my-sort-with #'some-function-to-sort-stuff list))
>         ((every #'numberp list)
>          (my-sort-with #'< list))
>         (t nil)))

You need extra walks through the list. There should be a way to
avoid this.
From: Thomas A. Russ
Subject: Re: Help with error handling
Date: 
Message-ID: <ymig1dqzonh.fsf@sevak.isi.edu>
····@hotmail.com writes:

> 
> So my real question is, is there anyway I could exit OUT of the recursive
> function to the toplevel and return NIL whenever any error occurs deep inside
> the recursive loop.

The easiest way to do this is does in fact use ignore-errors, but you
need to be a bit clever in where you put that form.  The essence is that
you want to have the top-level call of your recursive function be
different than internal calls.  That suggests the use of a "helper"
function to actually do the recursion:

(defun my-function (item-list)
  (ignore-errors (my-function-helper item-list)))

(defun my-function-helper (item-list)
   ... Here is where your recursive code goes ...)


This has the drawback of introducing an extra top-level function into
your namespace, but that might not be too bad -- it at least lets you
call the recursive function itself outside of the ignore-errors wrapper
for debugging purposes.

It is also possible using the LABELS construct to make the helper
function visible only inside the function:

(defun my-function (item-list)
  (labels ((my-function-helper (item-list)
              ... Here is where your recursive code goes ...))
    (ignore-errors (my-function-helper item-list))))


-- 
Thomas A. Russ,  USC/Information Sciences Institute          ···@isi.edu