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
>>>>> 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.
······@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)
(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
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."
"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")
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
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
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
* 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.
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
"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