From: Jordon Hirshon
Subject: Help
Date: 
Message-ID: <xeZS9.33505$p_6.2823368@bgtnsc04-news.ops.worldnet.att.net>
I'm new to Lisp.  I come from a C/C++ background.  How do I write a nested
loop, as in;

for(int i=0; i<5; i++)
   for(int j=0; j<5; j++)
      printf("%d %d\n", i, j);

Thanks,
Jordon

From: Christopher C. Stacy
Subject: Re: Help
Date: 
Message-ID: <u7kdfv6fr.fsf@dtpq.com>
>>>>> On Wed, 08 Jan 2003 17:30:05 GMT, Jordon Hirshon ("Jordon") writes:

 Jordon> I'm new to Lisp.  I come from a C/C++ background.  How do I write a nested
 Jordon> loop, as in;

 Jordon> for(int i=0; i<5; i++)
 Jordon>    for(int j=0; j<5; j++)
 Jordon>       printf("%d %d\n", i, j);

For that particular loop, I would use DOTIMES or LOOP.
From: Espen Vestre
Subject: Re: Help
Date: 
Message-ID: <kwznqahf6q.fsf@merced.netfonds.no>
······@dtpq.com (Christopher C. Stacy) writes:

>  Jordon> for(int i=0; i<5; i++)
>  Jordon>    for(int j=0; j<5; j++)
>  Jordon>       printf("%d %d\n", i, j);
> 
> For that particular loop, I would use DOTIMES or LOOP.

I think it's a shame that you can't do (*) this within FORMAT ;-)

(*) consing up lists first doesn't count, of course.
-- 
  (espen)
From: Kenny Tilton
Subject: Re: Help
Date: 
Message-ID: <3E1C7060.1070004@nyc.rr.com>
(dotimes (i 5)
   (dotimes (j 5)
       (format t "~d ~d~&" i j)))

The more general looper is:

(do ((i 0 (1+ i))) ;; var ivalue nextvalue
     ((>= i 5)) ;; test is whether to exit, not continue as in C
    (format....))

Someone else can tell you about LOOP, which I do not use for religious 
reasons.

Jordon Hirshon wrote:
> I'm new to Lisp.  I come from a C/C++ background.  How do I write a nested
> loop, as in;
> 
> for(int i=0; i<5; i++)
>    for(int j=0; j<5; j++)
>       printf("%d %d\n", i, j);
> 
> Thanks,
> Jordon
> 
> 


-- 

  kenny tilton
  clinisys, inc
  http://www.tilton-technology.com/
  ---------------------------------------------------------------
"Cells let us walk, talk, think, make love and realize
  the bath water is cold." -- Lorraine Lee Cudmore
From: Matthew Danish
Subject: Re: Help
Date: 
Message-ID: <20030108151222.I12928@lain.cheme.cmu.edu>
On Wed, Jan 08, 2003 at 06:34:03PM +0000, Kenny Tilton wrote:
> (dotimes (i 5)
>    (dotimes (j 5)
>        (format t "~d ~d~&" i j)))

> Someone else can tell you about LOOP, which I do not use for religious 
> reasons.

DOTIMES is best here, but for the sake of completeness:

(loop for i from 0 below 5 do
      (loop for j from 0 below 5 do
            (format t "~d ~d~%" i j)))


Note: ~% means newline, but ~& means newline iff not in first column
      (in FORMAT strings)

-- 
; Matthew Danish <·······@andrew.cmu.edu>
; OpenPGP public key: C24B6010 on keyring.debian.org
; Signed or encrypted mail welcome.
; "There is no dark side of the moon really; matter of fact, it's all dark."
From: Coby Beck
Subject: Re: Help
Date: 
Message-ID: <avijs5$2b9i$2@otis.netspace.net.au>
"Matthew Danish" <·······@andrew.cmu.edu> wrote in message
··························@lain.cheme.cmu.edu...
> DOTIMES is best here, but for the sake of completeness:
>
> (loop for i from 0 below 5 do
>       (loop for j from 0 below 5 do
>             (format t "~d ~d~%" i j)))
>

FWIW, the "from 0" is not necesary...

(loop for i below 5 do
      (loop for j below 5 do
            (format t "~d ~d~%" i j)))

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: Jock Cooper
Subject: Re: Help
Date: 
Message-ID: <m3adiai690.fsf@jcooper02.sagepub.com>
Kenny Tilton <·······@nyc.rr.com> writes:

> Someone else can tell you about LOOP, which I do not use for religious
> reasons.

I initially learned Lisp from reading Graham's ANSI Common Lisp, and
for a while I avoided using LOOP.  So I got pretty comfortable with DO
and DO*.  Most people here seemed quite comfortable using LOOP, so
later I started using it (and ITER) and found that for me it is a huge
timesaver.  I only occasionally use need to use DO now.

Jock

http://www.fractal-recursions.com
From: Kenny Tilton
Subject: Re: Help
Date: 
Message-ID: <3E1DBD89.3040504@nyc.rr.com>
Jock Cooper wrote:
> Kenny Tilton <·······@nyc.rr.com> writes:
> 
> 
>>Someone else can tell you about LOOP, which I do not use for religious
>>reasons.
> 
> 
> I initially learned Lisp from reading Graham's ANSI Common Lisp, and
> for a while I avoided using LOOP.  So I got pretty comfortable with DO
> and DO*.  Most people here seemed quite comfortable using LOOP, so
> later I started using it (and ITER) and found that for me it is a huge
> timesaver.  I only occasionally use need to use DO now.

Well, I have been won over (ie, th religious objection is now 
historical) by loop's doing things fer free that I have to do manually 
to make things efficient in certain situations, but I just haven't taken 
time to learn the crappy faux-NL syntax.

I dislike LOOP not because of Graham, but because it does not have 
enough parentheses. :)



-- 

  kenny tilton
  clinisys, inc
  http://www.tilton-technology.com/
  ---------------------------------------------------------------
"Cells let us walk, talk, think, make love and realize
  the bath water is cold." -- Lorraine Lee Cudmore
From: Peter Seibel
Subject: Re: Help
Date: 
Message-ID: <m3wuldh33j.fsf@localhost.localdomain>
Kenny Tilton <·······@nyc.rr.com> writes:

> I dislike LOOP not because of Graham, but because it does not have
> enough parentheses. :)

Come on, Kenny; this is Lisp. You don't have to suffer with syntax you
don't like. ;-)

(defmacro ploop ((&rest var-clauses) &body body)
  "Make Kenny Tilton happy ;-"
  (let ((code '()))
    (push 'loop code)
    (dolist (clause var-clauses)
      (setq code (nconc code clause)))
    (dolist (part body)
      (setq code (nconc code (list (car part)) (cdr part))))
    code))

Okay, that's just a sketch. But it handles many cases. For example:

(ploop ((for x upto 10)) (do (format t "~A " x))) =>
0 1 2 3 4 5 6 7 8 9 10 
NIL


(ploop ((with x = 10) (for y upto 5)) (collect (* x y))) =>
(0 10 20 30 40 50)

(ploop ((with good and bad and ugly)
        (for y upto 5))
       (collect y into good) 
       (collect (* 2 y) into bad)
       (collect (* -1 y) into ugly)
       (finally (return (values good bad ugly)))) => 
(0 1 2 3 4 5)
(0 2 4 6 8 10)
(0 -1 -2 -3 -4 -5)

-Peter

-- 
Peter Seibel
·····@javamonkey.com
From: Erik Naggum
Subject: Re: Help
Date: 
Message-ID: <3251186467222935@naggum.no>
* Kenny Tilton
| I dislike LOOP not because of Graham, but because it does not have
| enough parentheses. :)

  The simple `loop� form has the usual amount of parentheses, you know.

-- 
Erik Naggum, Oslo, Norway

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.
From: Kenny Tilton
Subject: Re: Help
Date: 
Message-ID: <3E1ED123.4030004@nyc.rr.com>
Erik Naggum wrote:
> * Kenny Tilton
> | I dislike LOOP not because of Graham, but because it does not have
> | enough parentheses. :)
> 
>   The simple `loop� form has the usual amount of parentheses, you know.

Yes, as I wrote the above it occurred to me I should look into that. 
Someone did post here a ways back a translation of a typical loop 
expression into one in the simple form.


-- 

  kenny tilton
  clinisys, inc
  http://www.tilton-technology.com/
  ---------------------------------------------------------------
"Cells let us walk, talk, think, make love and realize
  the bath water is cold." -- Lorraine Lee Cudmore
From: Nils Goesche
Subject: Re: Help
Date: 
Message-ID: <ly3co3mt41.fsf@cartan.de>
"Jordon Hirshon" <·········@worldnet.att.net> writes:

> I'm new to Lisp.  I come from a C/C++ background.  How do I write a
> nested loop, as in;
> 
> for(int i=0; i<5; i++)
>    for(int j=0; j<5; j++)
>       printf("%d %d\n", i, j);

Do you know how to write a (simple, non-nested) loop?

Regards,
-- 
Nils G�sche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x0655CFA0