for(int i=0; i<5; i++)
for(int j=0; j<5; j++)
printf("%d %d\n", i, j);
Thanks,
Jordon
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
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.
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
> 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."
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")
> 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)
> 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
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. :)
> 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
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.
Thanks,
Jordon
> 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.
> 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
> 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
> 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