# Thread: Make a loop return the result for each time its body is obeyed

1. ## Make a loop return the result for each time its body is obeyed

Registered forum members do not see this ad.

Hi everyone,

Just trying to pass a list of points to a "pline" command.
The problem is num of points is initially unknown, though being calculated within a routine.
The following code (as smth to start with) evolves known num of points.
Code:
```(defun c:pltest6 ()
(setq pts (list '(0 0) '(0 10) '(10 10) '(10 20) '(20 20) '(20 30) '(30 30)))
(setq ptqty 7)
(setq ptnum 0)
(command "pline"
(car pts)
(pt_retr pts)
(pt_retr pts)
(pt_retr pts)
(pt_retr pts)
(pt_retr pts)
(pt_retr pts)
""
)
)
(defun pt_retr (lst)
(setq ptnum (1+ ptnum))
(repeat ptnum
(setq lst (cdr lst))
)
(setq pt (car lst))
)```
Repeat/foreach don't do, 'cause they return the value of the last iteration..
Now my Q is,
is it possible to modify the code above to use loop instead of repeat one and the same line (moreover, unknown number of times ),
OR some other approach would rather fix the task?

Dmitro

2. Code:
```(defun c:FOO (/ _FOO pts)

(defun _FOO (pts /)
(command "._pline") (foreach pt pts (command pt)) (command)
)

(if (setq pts '((0 0)(0 10)(10 10)(10 20)(20 20)(20 30)(30 30)))
(_FOO pts)
)
(princ)
)```

3. Example:
Code:
```(defun c:pline-example ( / lst )
(setq lst '((0 0) (0 10) (10 10) (10 20) (20 20) (20 30) (30 30)))
(apply 'command (append '("_.pline") lst '("")))
(princ)
)```

4. Or
Code:
`(command "_pline")(mapcar 'command pt)(command "_cl")`
to make them closed, of course osmode is set to 0 before

5. BlackBox, Lee Mac & fixo,

Thank you so much.
I see I need 1,000 x more practice to get accustomed to lisp.
Sorry if you found my question borring.

Regards,
Dmitro

P.S. Does there exist a Forum/Thread where LISP is compared with another languages?
E.g. "THAT's C/Pascal/Basic code - and THIS is how we do it in LISP".

6. Originally Posted by dak
P.S. Does there exist a Forum/Thread where LISP is compared with another languages?
E.g. "THAT's C/Pascal/Basic code - and THIS is how we do it in LISP".
You might ought to ask this in a separate thread. But note that AutoLISP and Visual LISP are not really LISP, but dialects of it intended specifically for AutoCAD. Also, LISP is not the same type of language, so if you want to do it "right", it may be better not to try to think too much in terms of such languages. BTW, interestingly, no one mentioned that while would be a substitute looping function for repeat when stepping through a variable number of items, or when the number is of no importance. Could this be because AutoLISP works differently than the other languages you mention?

7. Originally Posted by fixo
Or
Code:
`(command "_pline")(mapcar 'command pt)(command "_cl")`
to make them closed, of course osmode is set to 0 before
Code:
`(command "_pline")(apply 'command pts)(command "")`
Or were you just showing that there is yet another function option?

8. Originally Posted by dak
P.S. Does there exist a Forum/Thread where LISP is compared with another languages?
E.g. "THAT's C/Pascal/Basic code - and THIS is how we do it in LISP".
There is no specific place for that sort of comparison, but it makes logical sense to want to understand how to accomplish similar task in different languages... In terms of AutoCAD, methinks the most common is quickly becoming Visual LISP vs. .NET (predominantly C# from what I see, but also some VB.NET, and F#)... I suspect you might see more VBA now that VBA 7.1 is supported.

As an aside, only when I was adept at coding LISP did I make the leap into the deep end of the .NET API, teaching myself C# & VB... It is then that I truly realized just how elegant LISP code can be, despite it's performance hit, with it's utter lack of Transactions, DocumentLocks, static Fields, permissions, etc.... It made me appreciate LISP even more than I thought I already had.

If you really have something in mind that 'here's how I do this in LISP, how can I do the same in .NET?' etc., post a thread in the .NET forum, include your LISP code, and we'll help you out.

Cheers

9. neophoible,

You right this could not of course! ; post #6 is concerned

My problem was in fact that I had no idea of how I would "screw" loops to the "pline" command. And that was because since you used to code a great deal in some language, you have adopted its specific tricks. In this case I concluded list functions rule (which was not obvious for me when I raised this thread).

Regards,
Dmitro

10. Registered forum members do not see this ad.

Originally Posted by dak
neophoible,

You right this could not of course! ; post #6 is concerned

My problem was in fact that I had no idea of how I would "screw" loops to the "pline" command. And that was because since you used to code a great deal in some language, you have adopted its specific tricks. In this case I concluded list functions rule (which was not obvious for me when I raised this thread).

Regards,
Dmitro
Actually, what was not obvious to me, until I saw someone do it, is that you can split a command up with multiple calls to command. That was a great revelation for me. FWIW, here is a trivial example of how while could be used:
Code:
```(defun C:Q (/ P)
(command "_.PLINE")
(while
(setq P (getpoint))
(command P)
)
(command "")
)```