I need to do a simple iteration i.e.
X := 100
WHILE X > 0 DO
PRINT X;
X := X -1;
END;
I have this so far:
(defun myloop (x)
(loop
(progn (print x)
(setf x (1- x))))
but i cant work out where to put in an escape clause (until (= x 0)) .
Thanks in advance
Ian.
* "dalmas" <········@hotmail.com>
| (defun myloop (x)
| (loop
| (progn (print x)
| (setf x (1- x))))
You can lose the progn.
| but i cant work out where to put in an escape clause (until (= x 0)) .
(when (= x 0) (return))
escapes from the (simple) loop body.
If you want a more interesting view on loop, the extended loop offers
a much broader and more interesting approach, as in:
(loop for x from 100 above 0 do (print x))
However, in this particular case, you probably want a more specialized
form (although this prints in reverse order from what you specified):
(dotimes (x 100) (print x))
I suggest you return to that mental state where you don't know the
language (like learning your first language) and only after you learn
something in it, compare it with other languages you know, if you at
all find that necessary in this learning process. This won't take any
more time than trying to match things up before you understand them.
Quite the contrary, in fact.
#:Erik
--
ALGORITHM: a procedure for solving a mathematical problem in a finite
number of steps that frequently involves repetition of an operation.
ALGOREISM: a procedure for solving an electoral problem in a finite
number of steps that frequently involves repetition of an operation.
Thanks,
I cant use dotimes etc as i dont know how many iterations are needed, I am
moving along a string and using particular character as the escape.
The (when (= x 0) (return)) works well though,
Thanks again,
Ian.
Erik Naggum <····@naggum.net> wrote in message
·····················@naggum.net...
> * "dalmas" <········@hotmail.com>
> | (defun myloop (x)
> | (loop
> | (progn (print x)
> | (setf x (1- x))))
>
> You can lose the progn.
>
> | but i cant work out where to put in an escape clause (until (= x 0)) .
>
> (when (= x 0) (return))
>
> escapes from the (simple) loop body.
>
> If you want a more interesting view on loop, the extended loop offers
> a much broader and more interesting approach, as in:
>
> (loop for x from 100 above 0 do (print x))
>
> However, in this particular case, you probably want a more specialized
> form (although this prints in reverse order from what you specified):
>
> (dotimes (x 100) (print x))
>
> I suggest you return to that mental state where you don't know the
> language (like learning your first language) and only after you learn
> something in it, compare it with other languages you know, if you at
> all find that necessary in this learning process. This won't take any
> more time than trying to match things up before you understand them.
> Quite the contrary, in fact.
>
> #:Erik
> --
> ALGORITHM: a procedure for solving a mathematical problem in a finite
> number of steps that frequently involves repetition of an operation.
> ALGOREISM: a procedure for solving an electoral problem in a finite
> number of steps that frequently involves repetition of an operation.
"dalmas" <········@hotmail.com> writes:
> I need to do a simple iteration i.e.
>
> X := 100
> WHILE X > 0 DO
> PRINT X;
> X := X -1;
> END;
>
> I have this so far:
>
> (defun myloop (x)
> (loop
> (progn (print x)
> (setf x (1- x))))
>
> but i cant work out where to put in an escape clause (until (= x 0)) .
Literally this:
(LOOP WHILE (> X 0)
DO (PRINT X)
(DECF X))
or
(LOOP UNTIL (<= X 0)
DO (PRINT X)
(DECF X))
(DECF X) is the same as (SETQ X (- X 1)) or (SETQ X (1- X)) and
is sort of the analog of --x in C-like languages.
If you don't like the loop keywords, you can write:
(LOOP (UNLESS (> X 0) (RETURN))
(PRINT X)
(DECF X))
or
(LOOP (WHEN (<= X 0) (RETURN))
(PRINT X)
(DECF X))
See the Common Lisp HyperSpec for (many) more examples.
It's at:
http://www.xanalys.com/software_tools/reference/HyperSpec/FrontMatter/
I don't like to give pointers into the document, but if you go to that
page, from there you can select the chapter index and look under Iteration
or the symbol index and look under LOOP. Good luck.
Hope this helps.
--Kent