Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help

7 views
Skip to first unread message

Jordon Hirshon

unread,
Jan 8, 2003, 12:30:05 PM1/8/03
to
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


Nils Goesche

unread,
Jan 8, 2003, 12:47:10 PM1/8/03
to
"Jordon Hirshon" <j.hi...@worldnet.att.net> writes:

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

Christopher C. Stacy

unread,
Jan 8, 2003, 1:32:09 PM1/8/03
to
>>>>> 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.

Kenny Tilton

unread,
Jan 8, 2003, 1:34:03 PM1/8/03
to
(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.


--

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

Matthew Danish

unread,
Jan 8, 2003, 3:12:22 PM1/8/03
to
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 <mda...@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."

Coby Beck

unread,
Jan 8, 2003, 8:41:13 PM1/8/03
to

"Matthew Danish" <mda...@andrew.cmu.edu> wrote in message
news:2003010815...@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")


Espen Vestre

unread,
Jan 9, 2003, 3:59:09 AM1/9/03
to
cst...@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)

Jock Cooper

unread,
Jan 9, 2003, 12:26:51 PM1/9/03
to
Kenny Tilton <kti...@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

Kenny Tilton

unread,
Jan 9, 2003, 1:15:30 PM1/9/03
to

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. :)

Peter Seibel

unread,
Jan 10, 2003, 2:32:56 AM1/10/03
to
Kenny Tilton <kti...@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
pe...@javamonkey.com

Erik Naggum

unread,
Jan 10, 2003, 6:21:07 AM1/10/03
to
* 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.

Kenny Tilton

unread,
Jan 10, 2003, 8:53:15 AM1/10/03
to

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.

Jordon Hirshon

unread,
Jan 15, 2003, 4:14:27 PM1/15/03
to
How can I turn a string into a list of symbols? (Not a list of characters).

Thanks,
Jordon


Edi Weitz

unread,
Jan 15, 2003, 4:27:17 PM1/15/03
to
"Jordon Hirshon" <j.hi...@worldnet.att.net> writes:

> How can I turn a string into a list of symbols? (Not a list of characters).

* (map 'list (lambda (c) (intern (string c))) "Foobar")
(F |o| |o| |b| |a| |r|)

And there a couple of other ways to do this as well. But are you sure
you want/need this?

Edi.

Henrik Motakef

unread,
Jan 15, 2003, 4:36:17 PM1/15/03
to
"Jordon Hirshon" <j.hi...@worldnet.att.net> writes:

> How can I turn a string into a list of symbols? (Not a list of characters).

Well, depends on the exact semantics you want.

Anyway, look up the function INTERN in the hyperspec:

| * (intern "FOO")
| FOO
| NIL
| * (symbolp (intern "BAR"))
| T
| *
| * (eq (intern "BAZ") 'baz)
| T

And perhaps SPLIT-SEQUENCE may be useful, see
<http://www.cliki.net/split-sequence>.

hth
Henrik

Adam Warner

unread,
Jan 15, 2003, 4:40:35 PM1/15/03
to
Hi Jordon Hirshon,

> How can I turn a string into a list of symbols? (Not a list of characters).

(loop for char across string collect (intern (string char)))

Regards,
Adam

Eduardo Muñoz

unread,
Jan 15, 2003, 5:31:50 PM1/15/03
to
"Jordon Hirshon" <j.hi...@worldnet.att.net> writes:

> How can I turn a string into a list of symbols? (Not a list of characters).

Something like this?


[12]> (let ((string "foo bar baz"))
(read-from-string (concatenate 'string "(" string ")")))
(FOO BAR BAZ) ;
13


You may want to bind *print-eval*


--

Eduardo Muñoz

0 new messages