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
"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
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.
"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")
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! =-----