From: Robin Boswell
Subject: Elementary question: returning from within a function
Date: 
Message-ID: <5s7e8d$fil$1@roadkill.scms.rgu.ac.uk>
I'm using Allegro Common Lisp, but I think the question is 
fairly general.

   Is there a recommended LISP cliche for handling the
case where I wish to test for a certain condition
at the start of a function, and if so, exit immediately?
(i.e. if I was writing in C, I would just use "return")
I can see various ways to do it, but they're all a 
bit messy.  
i) (defun foo (...)
      (cond ((test)
             (...)) ;; return appropriate value
            (t
            ...  ;; Here's the meat of the function
            ...
      )))
             
which is a bit clumsy, as it buries most of the function 
inside a cond condition.  This can be avoided by 
defining a second function foo2:

ii)  (defun foo (...)
      (cond ((test)
             (...)) ;; return appropriate value
            (t
            (foo2) ;; The meat of the function is now moved
                   ;; to a second function foo2
      )))

     (defun foo2 (...)
      ...
     )

This has the disadvantage that it requires two functions
to do what may basically be one "thing"

Finally, I can use return, but that seems to require
an extra block definition.

iii) (defun foo (...)
      (block nil
        (when (test)
           (return (...))) ;; return appropriate value
        ...   ;; Rest of function
        ...        
   
      ))

This strikes me as more elegant than i) or ii),
but having to enclose the entire function contents
in an extra block is still a bit messy.  Is there
a better way?

                Thanks,

                 Robin Boswell.

From: John Carroll
Subject: Re: Elementary question: returning from within a function
Date: 
Message-ID: <john.carroll-0608971013000001@johnca.crn.cogs.susx.ac.uk>
In article <············@roadkill.scms.rgu.ac.uk>, ···@scms.rgu.ac.uk (Robin Boswell) wrote:

>    Is there a recommended LISP cliche for handling the
> case where I wish to test for a certain condition
> at the start of a function, and if so, exit immediately?
> (i.e. if I was writing in C, I would just use "return")
> I can see various ways to do it, but they're all a 
> bit messy.  
> i) (defun foo (...)
>       (cond ((test)
>              (...)) ;; return appropriate value
>             (t
>             ...  ;; Here's the meat of the function
>             ...
>       )))
>...

The whole of the function body is wrapped in an implicit block
whose name is the name of the function itself. So you can do e.g.

(defun foo (x) (when x (return-from foo 'gg)) 'hh)
FOO

(foo t)
GG

(foo nil)
HH

John
-- 
School of Cognitive and Computing Sciences,
University of Sussex,
Falmer, Brighton BN1 9QH, UK

E-Mail: ············@cogs.susx.ac.uk
Tel: (+44 / 0)1273 678564 / Fax: (+44 / 0)1273 671320
From: Christopher J. Vogt
Subject: Re: Elementary question: returning from within a function
Date: 
Message-ID: <vogt-0608971012150001@204.248.25.27>
In article <············@roadkill.scms.rgu.ac.uk>, ···@scms.rgu.ac.uk
(Robin Boswell) wrote:

> I'm using Allegro Common Lisp, but I think the question is 
> fairly general.
> 
>    Is there a recommended LISP cliche for handling the
> case where I wish to test for a certain condition
> at the start of a function, and if so, exit immediately?
> (i.e. if I was writing in C, I would just use "return")
> I can see various ways to do it, but they're all a 
> bit messy.  
> i) (defun foo (...)
>       (cond ((test)
>              (...)) ;; return appropriate value
>             (t
>             ...  ;; Here's the meat of the function
>             ...
>       )))

Generally, if there are only 2 cases, I use "if" rather than "cond".

(defun foo (...)
   (if (test)
       (...) ; return value
     (... ; here is the meat of the function
       )))

[...]

-- 
···········@novia.net
Omaha, NE
http://www.novia.net/~vogt/
From: Barry Margolin
Subject: Re: Elementary question: returning from within a function
Date: 
Message-ID: <5sdmch$rtg@tools.bbnplanet.com>
In article <············@sparky.franz.com>, Kelly Murray <···@franz.com> wrote:
>I actually prefer, and support in my web scripting extention language,
>functions to have an implicit nil block, so one can use just return,
>but other CL'ers probably barf at this as too "undisciplined".

I think this was considered during the design of Common Lisp, but they
decided against it.  Since functions generally have a name, it seemed more
natural to use that as the block name.  The constructs that generate
implicit NIL blocks do so because they're anonymous constructs, so there's
no better name for them to use (although one possibility would be for the
block to be named after the construct, e.g. DO would create a block named
DO, LOOP would create a block named LOOP).  Another reason for the implicit
NIL block in these constructs is for compatibility with Maclisp, which only
had RETURN, not RETURN-FROM (I believe this was a Lisp Machine Lisp
addition).

-- 
Barry Margolin, ······@bbnplanet.com
BBN Corporation, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
Please don't send technical questions directly to me, post them to newsgroups.