From: Jason Stadnyk
Subject: Do* loops, need help if possible.
Date: 
Message-ID: <38ae2783.27648055@news>
Greeting,

I respect your policy towards students trying to flood this newsgroup
of requests for help, but I'm having trouble with implimentation of a
DO loop and any help or support would be greatly appreciated.

from what I can gather the normal notation for a do loop would be
this:

(do*
	((var1 initvalue (increment value process))
	 (var 2 initvalue (increment value process))
	..
	((end process) value returned or processed here))

now my problem is I have to do a cond statement list for each var1 in
a long list, where would it go. if I put it in the (processed here)
location, it would only be executed at the end.

and say Var2 is used to hold data, if the increment value would be a
conditional statement (only true if X = y add 1), would that cause an
error?


thanx in advance...


Jason Stadnyk
·····@sfn.saskatoon.sk.ca

From: Eugene Zaikonnikov
Subject: Re: Do* loops, need help if possible.
Date: 
Message-ID: <88md8a$r55$1@nnrp1.deja.com>
In article <·················@news>,
  ·····@sfn.saskatoon.sk.ca (Jason Stadnyk) wrote:
[snip]
> from what I can gather the normal notation for a do loop would be
> this:
>
> (do*
> 	((var1 initvalue (increment value process))
> 	 (var 2 initvalue (increment value process))
> 	..
> 	((end process) value returned or processed here))
>
> now my problem is I have to do a cond statement list for each var1 in
> a long list, where would it go. if I put it in the (processed here)
> location, it would only be executed at the end.
>
Put it after the whole termination clause, i.e:
... processed here)
(cond <your stuff>))

> and say Var2 is used to hold data, if the increment value would be a
> conditional statement (only true if X = y add 1), would that cause an
> error?
>
It wouldn't. Try this:
(do* ((var1 1 (1+ var1))
      (var2 1 (+ var2 (if (oddp var1)
                           1
                        0))))
     ((= 10 var1) var2))

--
  Eugene.


Sent via Deja.com http://www.deja.com/
Before you buy.
From: Pierre R. Mai
Subject: Re: Do* loops, need help if possible.
Date: 
Message-ID: <87snypgsrr.fsf@orion.dent.isdn.cs.tu-berlin.de>
·····@sfn.saskatoon.sk.ca (Jason Stadnyk) writes:

> from what I can gather the normal notation for a do loop would be
> this:
> 
> (do*
> 	((var1 initvalue (increment value process))
> 	 (var 2 initvalue (increment value process))
> 	..
> 	((end process) value returned or processed here))

You are missing the body part.  The following exceprt is taken from
the HyperSpec (see http://www.harlequin.com/education/books/HyperSpec/ 
for the on-line copy and information on downloading a copy for use
off-line), which will come in very handy in working with the
language.  Excerpts from the entry for do*:

"
Macro DO, DO*



Syntax:


do ({var | (var [init-form [step-form]])}*) (end-test-form result-form*)
declaration* {tag | statement}*

=> result*


do* ({var | (var [init-form [step-form]])}*) (end-test-form result-form*)
declaration* {tag | statement}*

=> result*

[...]

At the beginning of each iteration, after processing the variables, the
end-test-form is evaluated. If the result is false, execution proceeds with
the body of the do (or do*) form. If the result is true, the result-forms are
evaluated in order as an implicit progn, and then do or do* returns.

[...]

The remainder of the do (or do*) form constitutes an implicit tagbody. Tags
may appear within the body of a do loop for use by go statements appearing in
the body (but such go statements may not appear in the variable specifiers,
the end-test-form, or the result-forms). When the end of a do body is reached,
the next iteration cycle (beginning with the evaluation of step-forms) occurs.

"

Here's an example that does URL-style escaping on a string:

      (do* ((length (length string))
	    (result (make-string (+ length (* escape-count 2))))
	    (result-pos 0 (1+ result-pos))
	    (source-pos 0 (1+ source-pos)))
	   ((= source-pos length) result)
	(declare (type (string-inner-index 3) result-pos source-pos))
	(let ((char (schar string source-pos)))
	  (cond
	    ((escape-p char)
	     (escape-char char result result-pos)
	     (incf result-pos 2))
	    (t
	     (setf (schar result result-pos) char)))))



> now my problem is I have to do a cond statement list for each var1 in
> a long list, where would it go. if I put it in the (processed here)
> location, it would only be executed at the end.

Tuck the cond form (that's what you call your statement list) into the 
body of the do* form (see example above), and it will be executed on
each iteration after the variables have been assigned their values and 
the end-test form has evaluated to false.

> and say Var2 is used to hold data, if the increment value would be a
> conditional statement (only true if X = y add 1), would that cause an
> error?

The "increment value process" is really the variable-stepping form,
and the variable is bound to the value it returns.  How it computes
this value is up to the variable-stepping form, i.e. you can use any
conditional you want, e.g.

(defun count-items (list ignored-items)
  (do* ((counter 0 (if (member item ignored-items)
                       counter
                       (1+ counter)))
        (remainder list (rest remainder))
        (item (first remainder) (first remainder)))
       ((null remainder) counter)))

Regs, Pierre.

-- 
Pierre Mai <····@acm.org>         PGP and GPG keys at your nearest Keyserver
  "One smaller motivation which, in part, stems from altruism is Microsoft-
   bashing." [Microsoft memo, see http://www.opensource.org/halloween1.html]
From: Thomas A. Russ
Subject: Re: Do* loops, need help if possible.
Date: 
Message-ID: <ymiwvnwyenb.fsf@sevak.isi.edu>
·····@sfn.saskatoon.sk.ca (Jason Stadnyk) writes:

> 
> Greeting,
> from what I can gather the normal notation for a do loop would be
> this:
> 
> (do*
> 	((var1 initvalue (increment value process))
> 	 (var 2 initvalue (increment value process))
> 	..
> 	((end process) value returned or processed here))
> 
> now my problem is I have to do a cond statement list for each var1 in
> a long list, where would it go. if I put it in the (processed here)
> location, it would only be executed at the end.
> 
> and say Var2 is used to hold data, if the increment value would be a
> conditional statement (only true if X = y add 1), would that cause an
> error?

No.  Try it an see.

The DO form is fairly general.  You can pretty much use any legal Lisp
form to compute the new value for your iteration variable.  Note that if
you have a form for the new value, then the result of that form will be
assigned to the variable.  That means that if you wish the value to
remain unchanged, you will need to return the value itself:

(do* ((i 0 (1+ i))
      (j 0 (if (oddp i) (1+ j) j)))
   ((> i 10) j))

=> 6

-- 
Thomas A. Russ,  USC/Information Sciences Institute          ···@isi.edu