[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

return from loop but not function

james

11/5/2015 9:15:00 AM

Is there any way that exit from loop but not from the function?

(defun test()
(loop named ll
for i from 0 to 2
(return-from ll 1))
(print 'end))

The compiler will report "note: deleting unreachable code"

10 Answers

RamRod Sword of Baal

6/16/2011 5:30:00 AM

0



"IlBeBauck@gmail.com" wrote in message
news:de694816-a09d-4bf0-b1b9-5a2db6b285c1@v5g2000yqn.googlegroups.com...

On Jun 15, 12:59 pm, <ram...@truthonly.com> wrote:
>> Prophets of doom have been predicting the end of the earth forever, we
>> recently saw some here in these areas predicting it would end in various
>> dates in May, but here we are still going...................


> No, its the Creator who wants you to escape certain eternal regret .



Just because you screwed up your life, others will need follow your lead.

I for one live a happy life with my partner of over 25 years now.

We live in a modest air conditioned brick home in the tropics, a nice
garden, a swimming pool, we both have cars and motorcycles. We have a dog
and live a easy life, enjoying a cocktail at night before a fairly good
meal. We tend to cook quite a variety of international dishes.

We are not interested at all in your ravings about hell and the like.

I suggests you take your rubbish to the uneducated who might believe you.

Bye bye

William James

11/5/2015 10:29:00 AM

0

james wrote:

> Is there any way that exit from loop but not from the function?
>
> (defun test()
> (loop named ll
> for i from 0 to 2
> (return-from ll 1))
> (print 'end))
>
> The compiler will report "note: deleting unreachable code"

You don't say what language you are using. It looks
very barbarous ("defun"?).

Gauche Scheme:

(use srfi-42 :only (do-ec))

(define (test)
(do-ec
(:until (:range i 9) (= i 3))
(print i))
(print 'End))

(test)
0
1
2
3
End


Racket Scheme:

(define (test)
(let/ec break
(for ((i 3))
(displayln i)
(break)))
(displayln 'End))


> (test)
0
End

--
Sweden is already a banana republic, perhaps on its way to becoming an Islamic
republic. Swedish culture is disappearing with astonishing speed in front of
our eyes. If the trend isn't stopped, the Swedish nation will simply cease to
exist in any meaningful way during the first half of this century.
fjordman.blogspot.ca/2005/05/is-swedish-democracy-collapsing.html

Norbert_Paul

11/5/2015 11:08:00 AM

0

james wrote:
> Is there any way that exit from loop but not from the function?
Yes:
> (defun test()
> (loop named ll
> for i from 0 to 2
for i from 0 to 2 do
> (return-from ll 1))
> (print 'end))

Norbert_Paul

11/5/2015 11:18:00 AM

0

james wrote:
> Is there any way that exit from loop but not from the function?
Yes:
> (defun test()
> (loop named ll
> for i from 0 to 2
do
> (return-from ll 1))
> (print 'end))

Antsan

11/5/2015 12:18:00 PM

0

Am Donnerstag, 5. November 2015 10:15:02 UTC+1 schrieb james:
> Is there any way that exit from loop but not from the function?
>
> (defun test()
> (loop named ll
> for i from 0 to 2
> (return-from ll 1))
> (print 'end))
>
> The compiler will report "note: deleting unreachable code"
You need a `do` there before `(return-from ll 1)`. What you've got is not
standard-compliant, as, as it is, the parenthesized part has no meaning.

Antsan

11/5/2015 12:23:00 PM

0

Am Donnerstag, 5. November 2015 10:15:02 UTC+1 schrieb james:
> Is there any way that exit from loop but not from the function?
>
> (defun test()
> (loop named ll
> for i from 0 to 2
> (return-from ll 1))
> (print 'end))
>
> The compiler will report "note: deleting unreachable code"

Regarding the "deleting unreachable code":
It might be that your CL implementation assume the missing `do`.
As the `1` returned by `return-from` is never used for anything, that is
probably the deleted code. Actually probably the whole loop might be optimized
away. It doesn't do anything but instead immediately exits.

Kaz Kylheku

11/5/2015 1:25:00 PM

0

On 2015-11-05, james <dinglei2008@gmail.com> wrote:
> Is there any way that exit from loop but not from the function?
>
> (defun test()
> (loop named ll
> for i from 0 to 2
> (return-from ll 1))
> (print 'end))
>
> The compiler will report "note: deleting unreachable code"

You left out a key piece of information: is the (print 'end)
evaluating or not?

If END is printed, then the loop is obviously not bailing out of the function.

If it isn't printed, there is a serious problem going on, because the block
named ll should be confined to loop's expansion and not magically stretch over
the (print 'end).

The (return-from ...) clause requires a do.

CLISP diagnoses it as bad syntax if we don't have the do there;
the loop form is entirely rejected with an error.

The (return-from ...) form is syntactically dangling and in your implemenation,
quite possibly, this dangling form is getting inserted into the macro-expansion
of the loop in such a way that it is unreachable code, and is reported as such
by the later compilation pass.

We *can* have forms in loop without a do: in the simple form of the loop
which looks like this:

(loop (form1 ...) (form2 ...))

I.e. the loop symbol is immediately followed by compound forms.

Pascal J. Bourguignon

11/5/2015 1:48:00 PM

0

Kaz Kylheku <kaz@kylheku.com> writes:

> On 2015-11-05, james <dinglei2008@gmail.com> wrote:
>> Is there any way that exit from loop but not from the function?
>>
>> (defun test()
>> (loop named ll
>> for i from 0 to 2
>> (return-from ll 1))
>> (print 'end))
>>
>> The compiler will report "note: deleting unreachable code"
>
> You left out a key piece of information: is the (print 'end)
> evaluating or not?
>
> If END is printed, then the loop is obviously not bailing out of the function.
>
> If it isn't printed, there is a serious problem going on, because the block
> named ll should be confined to loop's expansion and not magically stretch over
> the (print 'end).

And remember that printing is effective on the device only after you've
flushed the buffers, that is, after you've called finish-output
(synchronous) or force-output (asynchronous).

So:

(defun test()
(loop named ll
for i from 0 to 2
do (print 'in-loop)
do (return-from ll 1))
(print 'end)
(terpri)
(finish-output))


cl-user> (test)

in-loop
end
nil

> We *can* have forms in loop without a do: in the simple form of the loop
> which looks like this:
>
> (loop (form1 ...) (form2 ...))
>
> I.e. the loop symbol is immediately followed by compound forms.

And when you use explicitely return to exit from the loop you can indeed
use this simple form.

(defun test()
(let ((i 0))
(loop
(incf i)
(print 'loop-head)
(unless (< i 1) (return 1))
(print 'loop-tail)))
(print 'end)
(terpri)
(finish-output))


cl-user> (test)

loop-head
end
nil
cl-user>

--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

james

11/6/2015 2:49:00 AM

0

On Thursday, November 5, 2015 at 5:15:02 PM UTC+8, james wrote:
> Is there any way that exit from loop but not from the function?
>
> (defun test()
> (loop named ll
> for i from 0 to 2
> (return-from ll 1))
> (print 'end))
>
> The compiler will report "note: deleting unreachable code"

Thanks for all your answer :)
1>Yes, I missed a do before (return)
2>Glad to know the usage (loop (form1 ...) (form2 ...))
3>After expand the loop I got to understand the error which compiler said. I am using sbcl
(defun test()
(loop
for i from 0 to 2
do (return 1))
(print 'end)
)

macro-expand
(BLOCK NIL
(LET ((I 0))
(DECLARE (TYPE (AND REAL NUMBER) I))
(TAGBODY
SB-LOOP::NEXT-LOOP
(IF (> I '2)
(PROGN (GO SB-LOOP::END-LOOP))
NIL)
(PRINT 1)
(RETURN-FROM NIL 1)
(SETQ I (1+ I))
(GO SB-LOOP::NEXT-LOOP)
SB-LOOP::END-LOOP)))

; file: /private/var/tmp/tmp.bT58OB
; in: DEFUN TEST
; (LOOP FOR I FROM 0 TO 2
; DO (RETURN 1))
; --> BLOCK LET SB-LOOP::LOOP-BODY TAGBODY SB-LOOP::LOOP-REALLY-DESETQ
; --> SETQ THE 1+ +
; ==>
; I
;
; note: deleting unreachable code
;
; compilation unit finished
; printed 1 note

taruss

11/6/2015 10:01:00 PM

0

On Thursday, November 5, 2015 at 1:15:02 AM UTC-8, james wrote:
> Is there any way that exit from loop but not from the function?
>
> (defun test()
> (loop named ll
> for i from 0 to 2
> (return-from ll 1))
> (print 'end))
>
> The compiler will report "note: deleting unreachable code"

I will also note that there is the (LOOP-FINISH) form, which will terminate the loop but also run any FINALLY code. It will not return a value unless the loop is using one of the forms that would normally cause it to return a value.

Using RETURN-FROM as above will only return a value when the RETURN-FROM is executed. If this is done only conditionally (otherwise, why bother with the loop), then if the loop terminates normally it will return NIL.