From: Dai Yuwen
Subject: use `values' to return immediately from a function
Date: 
Message-ID: <atk1co$1cn7$1@mail.cn99.com>
Hi, All

I found `values' can not return immediately from the middle of a function 
just like `return'.  For example:

(defun test_values () (values 2 3 ) (print #\a))

The print will be still executed.
So how do I return multiple values in the middle of a function?

Best regards,
Dai Yuwen

From: Geoffrey Summerhayes
Subject: Re: use `values' to return immediately from a function
Date: 
Message-ID: <thgL9.1199$iQ3.335972@news20.bellglobal.com>
"Dai Yuwen" <·····@micetek.com.cn> wrote in message
··················@mail.cn99.com...
> Hi, All
>
> I found `values' can not return immediately from the middle of a function
> just like `return'.  For example:
> (defun test_values () (values 2 3 ) (print #\a))
>
> The print will be still executed.
> So how do I return multiple values in the middle of a function?

RETURN will only exit a block named NIL. This block is implictly declared
by constructs like DO.

: (defun foo() (+ 3 4 (block nil (return 6))))
FOO
: (foo)
13

DEFUN function-name creates a block named function-name.
(RETURN-FROM function-name ...) will exit that block.

: (defun foo() (+ 3 4 (block nil (return-from foo 6))))
FOO
: (foo)
6

(defun test_values () (return-from test_values (values 2 3)) (print #\a))

--
Geoff
From: Kaz Kylheku
Subject: Re: use `values' to return immediately from a function
Date: 
Message-ID: <cf333042.0212161124.7c4ef7a3@posting.google.com>
Dai Yuwen <·····@micetek.com.cn> wrote in message news:<·············@mail.cn99.com>...
> Hi, All
> 
> I found `values' can not return immediately from the middle of a function 
> just like `return'. 

That's because it's not a return mechanism; it's a function that
computes a multiple value out of its arguments.

> For example:
> 
> (defun test_values () (values 2 3 ) (print #\a))
> 
> The print will be still executed.
> So how do I return multiple values in the middle of a function?

DEFUN is a macro which inserts your code into a block that is named by
the function symbol, as if you had written:

   (block test_values 
     (values 2 3) (print #\a))

Because of this, you can use RETURN-FROM to break out of the function
by specifying the block name:

   (return-from test_value (values 2 3))

Of course, if you ever refactor the code by moving pieces from one
function to another, this will screw up. There are other ways to
achieve the effect. Like your own anonymous block:

   (block nil
     ...
     (return (values 2 3))
     ...)

It's usually worthwhile to rack your brains a little longer, and come
up with a way to avoid coding an explicit return. There are plenty of
control constructs in the language: if, case, cond, loop, etc.

By the way, why use underscores for compound names? It's more
idiomatic to use names like test-value. It's easier to type too
because you don't have to use the shift key.
From: Coby Beck
Subject: Re: use `values' to return immediately from a function
Date: 
Message-ID: <atk1r3$1ouv$1@otis.netspace.net.au>
"Dai Yuwen" <·····@micetek.com.cn> wrote in message
··················@mail.cn99.com...
> Hi, All
>
> I found `values' can not return immediately from the middle of a function
> just like `return'.  For example:
>
> (defun test_values () (values 2 3 ) (print #\a))
>
> The print will be still executed.
> So how do I return multiple values in the middle of a function?

CL-USER 8 > (defun foo ()
                 (return-from foo (values "Larry" "Curly" "Moe"))
                 (print (solve-mystery-of-the-universe)))
FOO

CL-USER 9 > (foo)
"Larry"
"Curly"
"Moe"


--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: sv0f
Subject: Re: use `values' to return immediately from a function
Date: 
Message-ID: <none-1612020941550001@129.59.212.53>
In article <·············@otis.netspace.net.au>, "Coby Beck"
<·····@mercury.bc.ca> wrote:

>CL-USER 8 > (defun foo ()
>                 (return-from foo (values "Larry" "Curly" "Moe"))
>                 (print (solve-mystery-of-the-universe)))
>FOO
>
>CL-USER 9 > (foo)
>"Larry"
>"Curly"
>"Moe"

Coby,

What happens when you type the following into your listener?

(defun foo ()
  (print (solve-mystery-of-the-universe))
  (return-from foo (values "Larry" "Curly" "Moe")))

(foo)

Just curious.
From: Larry Clapp
Subject: Re: use `values' to return immediately from a function
Date: 
Message-ID: <1m3lta.uq6.ln@127.0.0.1>
In article <·····················@129.59.212.53>, sv0f wrote:
> In article <·············@otis.netspace.net.au>, "Coby Beck"
><·····@mercury.bc.ca> wrote:
>>CL-USER 8 > (defun foo ()
>>                 (return-from foo (values "Larry" "Curly" "Moe"))
>>                 (print (solve-mystery-of-the-universe)))
>>FOO
>>
>>CL-USER 9 > (foo)
>>"Larry"
>>"Curly"
>>"Moe"
> 
> Coby,
> 
> What happens when you type the following into your listener?
> 
> (defun foo ()
>   (print (solve-mystery-of-the-universe))
>   (return-from foo (values "Larry" "Curly" "Moe")))
> 
> (foo)
> 
> Just curious.

The universe disappears and is replaced by something many times more weird and
inscrutable.  Obviously, Coby has run FOO a couple of times.  ;)

Or possibly he just gets an undefined function error.



-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----
From: Coby Beck
Subject: Re: use `values' to return immediately from a function
Date: 
Message-ID: <atlh3s$2e2v$1@otis.netspace.net.au>
"sv0f" <····@vanderbilt.edu> wrote in message
··························@129.59.212.53...
> In article <·············@otis.netspace.net.au>, "Coby Beck"
> <·····@mercury.bc.ca> wrote:
>
> >CL-USER 8 > (defun foo ()
> >                 (return-from foo (values "Larry" "Curly" "Moe"))
> >                 (print (solve-mystery-of-the-universe)))
> >FOO
> >
> >CL-USER 9 > (foo)
> >"Larry"
> >"Curly"
> >"Moe"
>
> Coby,
>
> What happens when you type the following into your listener?

That was yesterday's session, now I'm just getting an undefined function
error.

> (defun foo ()
>   (print (solve-mystery-of-the-universe))
>   (return-from foo (values "Larry" "Curly" "Moe")))
>
> (foo)
>
> Just curious.

Damn, I wish I'd saved that buffer...
;)


--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")