From: nabla
Subject: Re: lisp equivalent to c++ continue
Date: 
Message-ID: <d32c3$4348d6e9$540a2bbf$31624@news.chello.pl>
retupmoca napisał(a):
> What is the lisp equivalent to the following c++:
> 
> for(;;)
> {
>   continue;
>   break;    // will never get reached
> }
> 

You can use block/return-from special forms. Like in this example:

(defun test-1 ()
   (block break
     (dotimes (i 100)
       (block continue
	(when (oddp i)
	    (return-from continue))
	(when (= i 10)
	    (return-from break))
	(print i)))))

Looks ugly.


Fortunatelly do/dotimes have implicite features:


"
An implicit block named nil surrounds dotimes. return may be used to 
terminate the loop immediately without performing any further 
iterations, returning zero or more values.

The body of the loop is an implicit tagbody; it may contain tags to 
serve as the targets of go statements. Declarations may appear before 
the body of the loop. "

http://www.lisp.org/HyperSpec/Body/mac_dotimes.html

So you can write


(defun test-2 ()
   (dotimes (i 100)
     (when (oddp i)
       (go :continue))
     (when (= i 10)
       (return))
     (print i)
     :continue))

It looks better. At least for me. :-)



-- 
Best regards,
Rafal Strzalinski (nabla)

From: Kaz Kylheku
Subject: Re: lisp equivalent to c++ continue
Date: 
Message-ID: <1128875188.978965.315480@o13g2000cwo.googlegroups.com>
nabla wrote:
> (defun test-2 ()
>    (dotimes (i 100)
>      (when (oddp i)
>        (go :continue))
>      (when (= i 10)
>        (return))
>      (print i)
>      :continue))
>
> It looks better. At least for me. :-)

Or:

 (defun test-2 ()
   (dotimes (i 100)
     (cond
       ((oddp i))
       ((= i 10) (return))
       (t (print i)))))
From: Duane Rettig
Subject: Re: lisp equivalent to c++ continue
Date: 
Message-ID: <48xx1pawr.fsf@franz.com>
nabla <···············@nospam.gmail.com> writes:

> retupmoca napisa�(a):
>> What is the lisp equivalent to the following c++:
>> for(;;)
>> {
>>   continue;
>>   break;    // will never get reached
>> }
>>
>
> You can use block/return-from special forms. Like in this example:
>
> (defun test-1 ()
>    (block break
>      (dotimes (i 100)
>        (block continue
> 	(when (oddp i)
> 	    (return-from continue))
> 	(when (= i 10)
> 	    (return-from break))
> 	(print i)))))
>
> Looks ugly.

I actually prefer this solution above to the tagbody solution,
because it is higher-level than the tagbody construct.  However,
I would make two changes:

 1. The break block isn't necessary; (return) always returns from
the closest unnamed block (in this case, the do), and since you do it
also in your tagbody version later, it seems an unfair comparison to
add an extra block to the picture.

 2. Continue is a nondescriptive name for the iteration we're
aborting, and in a large program could even be misleading.

Taken together, the changed version looks like this:

(defun test-2 ()
  (dotimes (i 100)
    (block iteration
      (when (oddp i)
	(return-from iteration))
      (when (= i 10)
	(return))
      (print i))))

which looks much less ugly than the version above.  Whether it is
better than the tagbody approach is a matter of taste.

-- 
Duane Rettig    ·····@franz.com    Franz Inc.  http://www.franz.com/
555 12th St., Suite 1450               http://www.555citycenter.com/
Oakland, Ca. 94607        Phone: (510) 452-2000; Fax: (510) 452-0182