### Author Topic: nested "FOR NEXT"  (Read 6450 times)

0 Members and 1 Guest are viewing this topic.

#### JohnK

• Seagull
• Posts: 10625
##### Re: nested "FOR NEXT"
« Reply #15 on: October 27, 2010, 11:13:13 AM »
Dont mind me. I thought i saw something that wasnt really there i guess. I didnt know what "FOR NEXT" was so i was going off a...ah whatever, im nuts.

I built a tail recursive FORNEXT function that i was going to lead the OP to but i was approaching this more from the academic side because i thought that...*ah* yes, i realize im crazy so im just going to shut up and go away now.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

#### Robert98

• Guest
##### Re: nested "FOR NEXT"
« Reply #16 on: October 27, 2010, 01:06:40 PM »
I have read that some lisp codes can call itself like :
You are referring to recursive functions. I don't see recursive functions in your OP. We still don't know what it is you want to accomplish.

Hi Roy
At this example I want compare two list and remove unequal members. but it is just a sample apllication and may by other time I want compare two list for other pourpose such as save four digit numbers and remove reminder members and so on . I want learn If I have more thane one list that their length is'nt equal , how I can work with them simultanusly .for example I make a pline on a topographic map that it's z value is zero (in 2D space) at the other hand I have large amounts topographic text elevations and many contur , now I want filter cordinates of text and put it in list (for eample A) and then make a list of pline nodes (for eample B) , now supose I want compare two list and remove undisired element and finaly make a new list so that z value of text corespound to a node replaced by node z value !
for example :
text string elevation is : 1245.32
corespoun node to it string has coordinates : (1000.0 1000.0 0.0)
my desired node coordinates must be :  (1000.0 1000.0 1245.32)
I do not know that I could , get you my purpose with this topographic and pline example or no ?
please note that I don't have any project and topographics map now and I just want learn about:1- compare two or more lists to each other for a certain purpose 2- How I can handling two or more lists simultanusly that don't have equal members (perhaps equal) 3- for this aim , I must start with ....?
sorry for my bad english skill.

#### Robert98

• Guest
##### Re: nested "FOR NEXT"
« Reply #17 on: October 27, 2010, 01:24:13 PM »
*confused* Maybe i wasn't as far off as i thought.

I have a question Robert98.
Do you know another programming language--like C--and you are just trying to learn AutoLisp?

My original thoughts were based upon a feeling from the formatting of the code posted (it kinda reminded me of a class def) and a quick scan of that tut page (after reading it closer i think its just bad but i originality thought it was written for a C to AutoLisp conversion tut or something).
...meh, never mind me.

Hi
I can write VB and C++ and C but not professional , at a normal level

#### gile

• Gator
• Posts: 2507
• Marseille, France
##### Re: nested "FOR NEXT"
« Reply #18 on: October 27, 2010, 01:54:45 PM »
Hi,

MP shows you a way to do what you want reply #7 unsin the foreach function

Here's another way using the vl-remove-if-not built-in function. It's not a Visual LISP function using the COM/activeX interop, it's an AutoLISP function which came with Visual LISP.
As Vladimir Nesterowsky's remove_if_not many others vl-* functions have been defined as defun by AutoLISP pionners (Vladimir Nesterowsky, Reini Urban, ...).
You'd rather use the built-in vl-* functions than their old defun equivalents (but some of these are really intersting to study while you're learnig AutoLISP).

Code: [Select]
`(defun common (l1 l2)  (vl-remove-if-not    (function (lambda (x) (member x l2)))    l1  ))`
As you're talking about "lisp codes can call itself" (recursive processes which are a main pat of all LISP languages), here's another example using this method:

Code: [Select]
`(defun common (l1 l2)  (if l1    (if (member (car l1) l2)      (cons (car l1) (common (cdr l1) l2))      (common (cdr l1) l2)    )  ))`
With these lists
Code: [Select]
`(setq lis1 '(10 12 14 15 13)      lis2 '(74 1114 12 13 15 18))`
With both above 'common' routines, (common lis1 lis2) return (12 13 15)
« Last Edit: October 27, 2010, 01:57:55 PM by gile »
Speaking English as a French Frog

#### Robert98

• Guest
##### Re: nested "FOR NEXT"
« Reply #19 on: October 27, 2010, 05:34:58 PM »
Hi,

MP shows you a way to do what you want reply #7 unsin the foreach function

Here's another way using the vl-remove-if-not built-in function. It's not a Visual LISP function using the COM/activeX entrap, it's an AutoLISP function which came with Visual LISP.
As Vladimir Nesterowsky's remove_if_not many others vl-* functions have been defined as defun by AutoLISP pioneers (Vladimir Nesterowsky, Reini Urban, ...).
You'd rather use the built-in vl-* functions than their old defun equivalents (but some of these are really intersting to study while you're learning AutoLISP).

Code: [Select]
`(defun common (l1 l2)  (vl-remove-if-not    (function (lambda (x) (member x l2)))    l1  ))`
As you're talking about "lisp codes can call itself" (recursive processes which are a main pat of all LISP languages), here's another example using this method:

Code: [Select]
`(defun common (l1 l2)  (if l1    (if (member (car l1) l2)      (cons (car l1) (common (cdr l1) l2))      (common (cdr l1) l2)    )  ))`
With these lists
Code: [Select]
`(setq lis1 '(10 12 14 15 13)      lis2 '(74 1114 12 13 15 18))`
With both above 'common' routines, (common lis1 lis2) return (12 13 15)
Hi Gile
I'll worked with these codes tomorrow ! and now I have a question of you :

One of my friends give me a lot of tools from Reini Urban that start wit STD , I read many papers of them , but don't work with them , dose you know what these functions , I must install them before use or they loaded by other method such as ARX , I wrote one of them please tell me about them because most of them are short routines :

Code: [Select]
`;;; safe NTH version with reverse order of arguments;;; Throws an index out of range error.;;; NTH throw just a bad argument type error;;; (from LISP);;; (STD-ELT '(0 1) 0) = > 0;;; (STD-ELT lst -1) = > ERROR;;; (STD-ELT lst (length lst )) = > ERROR(defun STD-ELT (lst i)(if (< -1 i (length lst));fixed(nth i lst)(std-error (list "STD-ELT: "(std-msg ; |MSG2|;"index out of range")" -" i))));===================================================;;; std-select in NTH expanded for multiple indices.(from xlisp-stat);;;   (std-select lst '(0 2 4)) or (std-select lst 0)(defun STD-SELECT (lst i / x)(cond((numberp i) (std-elt lst i))((consp i)(while i ; looping over i is faster than mapping over lst(setq x (cons (std-elt lst (car i)) x)i (cdr i)))(reverse x)))) `please tell me about these since I think they are'nt need runing vl objects.

#### JohnK

• Seagull
• Posts: 10625
##### Re: nested "FOR NEXT"
« Reply #20 on: October 27, 2010, 09:37:40 PM »
The STL library Reini wrote would be like any other STL library in any other language except that you do not use it or portions of it like you do in other languages.

For example:
In C++ "#include <iostream>"  Includes the I/O stream header file(s). AutoLisp does not have a an "INCLUDE" statement. You would have to manually add code to locate and load the AutoLisp STL file (like the one you are using as an example) you wanted to include. It is much easier to use the AutoLisp STL as a learning tool and write your own.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

#### hermanm

• Guest
##### Re: nested "FOR NEXT"
« Reply #21 on: October 27, 2010, 10:50:26 PM »
Quote
AutoLisp does not have a an "INCLUDE" statement

At one time, AutoLISP might have progressed to that point.
Please see attached file, which shipped with R12.
AFAIK, it is a fake (read "aspiration")  & really isn't implemented per doc.
IOW the file is loaded in toto, not just the designated functions.
If I am mistaken, please correct me.

Implementing a real AutoLISP "include" is on my "to do" list.
Choice of programming language TBD - though I am leaning towards F#..
Would be a suitable challenge for me...
BTW, Anyone (else) here still use ALLY?

#### MP

• Seagull
• Posts: 17750
• Have thousands of dwgs to process? Contact me.
##### Re: nested "FOR NEXT"
« Reply #22 on: October 27, 2010, 11:07:13 PM »
This is similar to an include statement insomuch as one statement can load an entire library (or libraries) of supporting lisp source.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client

#### Robert98

• Guest
##### Re: nested "FOR NEXT"
« Reply #23 on: October 28, 2010, 02:17:59 PM »
Quote
AutoLisp does not have a an "INCLUDE" statement

At one time, AutoLISP might have progressed to that point.
Please see attached file, which shipped with R12.
AFAIK, it is a fake (read "aspiration")  & really isn't implemented per doc.
IOW the file is loaded in toto, not just the designated functions.
If I am mistaken, please correct me.

Implementing a real AutoLISP "include" is on my "to do" list.
Choice of programming language TBD - though I am leaning towards F#..
Would be a suitable challenge for me...
BTW, Anyone (else) here still use ALLY?

Dear hermanm
Thanks for your tips and very nice attached file . I think that with this function any body can load his small functions such as c++ that , dear Roy told. I don't further about this file since I'm beginner in lisp and must other master friend , tell about it . I must test it and wait for results ....!
Bay

#### Robert98

• Guest
##### Re: nested "FOR NEXT"
« Reply #24 on: October 28, 2010, 02:23:08 PM »
This is similar to an include statement insomuch as one statement can load an entire library (or libraries) of supporting lisp source.
Hi MP
I have a question about include.lsp :
At that file wrote that make a file with llb extension and I want know these llb file have lisp codes or other language codes ?
bay master

#### CAB

• Global Moderator
• Seagull
• Posts: 10401
##### Re: nested "FOR NEXT"
« Reply #25 on: October 31, 2010, 07:02:45 PM »
I'll add my 2 cents to the discussion.
Dealing with two lists is easy.
One way was described by MP but keep in mind that the member or vl-position functions
do not preform well with real numbers because of rounding.  Slight variations will cause
them to fail to find a match. Dealing with real numbers you need to use equal with a fuzz
or use the distance function when dealing with point lists.
Example:
Code: [Select]
`_\$ (equal '(585.039 339.407) '(585.039 339.407))T_\$ (equal '(585.03901 339.407) '(585.039 339.407))nil_\$ (equal '(585.03901 339.407) '(585.039 339.407) 0.0001)T`

There is another potential problem using foreachfunction. When the foreachfunction is
initialized the list is placed in another hidden variable or memory location and is not bound
to the original list. If you alter the list within the foreachconstruct the change will not
be evident to the foreachfunction.
Example:
Code: [Select]
`(setq lst '(1 2 3 4 5))(foreach itm lst  (if (= itm 2)    (setq lst (reverse (cdr (reverse lst))))  )  (print itm) (princ " - ")(princ lst))(princ)`
You see although lst was changed to 4 items the foreachdid 5 iteration's.

Comparing two lists of point lists you might want to use nested foreachfunctions.

Code: [Select]
`(defun c:test (/ lst1 lst2 p1 p2 matchList)  (setq lst1 '((546.541 279.55)  ; 2D point list               (585.039 339.407)               (540.008 394.517)               (473.68 368.72)               (477.717 297.667)              )  )  (setq lst2 '((546.541 279.55 12.5)  ; 3D point list               (585.039 339.407 15.6)               (540.008 394.517 22.7)              )  )  (defun 2d (pt)                        ; return a point list less the Z value    (list (car pt) (cadr pt))  )  (foreach p1 lst1    (foreach p2 lst2      (if (equal (2d p1) (2d p2) 0.0001)        (setq matchList (cons p2 matchList))      )    )  )  (print matchList)  (princ))`
You can see that lst2 is iterated each time an item in lst1 is processed.
With large lists it may be desirable to remove a point from lst2 when it is paired.
To accomplish this the use of a while function does the job. The while function
will not store the list value and therefore your change will shorten the processing.

Code: [Select]
`(defun c:test (/ lst1 lst2 p1 p2 idx matchList tmplst)  (setq lst1 '((546.541 279.55)  ; 2D point list               (585.039 339.407)               (540.008 394.517)               (473.68 368.72)               (477.717 297.667)              )  )  (setq lst2 '((546.541 279.55 12.5)  ; 3D point list               (585.039 339.407 15.6)               (540.008 394.517 22.7)              )  )  (defun 2d (pt)                        ; return a point list less the Z value    (list (car pt) (cadr pt))  )  (foreach p1 lst1    (setq tmplst lst2)    (while (setq p2 (car tmplst))      (setq tmplst (cdr tmplst))      (if (equal (2d p1) (2d p2) 0.0001)        (setq matchList (cons p2 matchList)              lst2 (vl-remove p2 lst2))      )    )  )  (print matchList)  (princ))`
You can also use a pointer with the while loop.

Code: [Select]
`(defun c:test (/ lst1 lst2 p1 p2 idx matchList)  (setq lst1 '((546.541 279.55)  ; 2D point list               (585.039 339.407)               (540.008 394.517)               (473.68 368.72)               (477.717 297.667)              )  )  (setq lst2 '((546.541 279.55 12.5)  ; 3D point list               (585.039 339.407 15.6)               (540.008 394.517 22.7)              )  )  (defun 2d (pt)                        ; return a point list less the Z value    (list (car pt) (cadr pt))  )  (foreach p1 lst1    (setq idx (length lst2))    (while (>= (setq idx (1- idx)) 0)      (setq p2 (nth idx lst2))      (if (equal (2d p1) (2d p2) 0.0001)        (setq matchList (cons p2 matchList)              lst2 (vl-remove p2 lst2))      )      (print lst2)    )  )  (print matchList)  (princ))`
You may wonder why the vl-remove works with point data, real numbers.
That only works because p2 is an exact copy of the data found in lst2.

That's it.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970

#### Lee Mac

• Seagull
• Posts: 12912
• London, England
##### Re: nested "FOR NEXT"
« Reply #26 on: October 31, 2010, 07:13:54 PM »
Nice explanation Alan, very informative

Just to add my little snippet to the mix, one could perhaps construct a 'member with fuzz', maybe something like:

Code: [Select]
`(defun _member ( item alist fuzz )  (vl-some (function (lambda ( _item ) (equal _item item fuzz))) alist))`
Code: [Select]
`_\$ (_member 585.039 '(585.03901 339.407) 0.0001)T`
Then, using the code MP kindly provided:

Code: [Select]
`(defun venn_inters ( list1 list2 / result )    (foreach element list1        (if (_member element list2 0.0001)            (setq result (cons element result))        )                )        (reverse result))`
Just a thought

Lee

#### CAB

• Global Moderator
• Seagull
• Posts: 10401
##### Re: nested "FOR NEXT"
« Reply #27 on: October 31, 2010, 09:55:38 PM »
Good solution Lee.

My post was for information as the op seemed to want that.

I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970

#### gile

• Gator
• Posts: 2507
• Marseille, France
##### Re: nested "FOR NEXT"
« Reply #28 on: November 01, 2010, 06:13:58 AM »
Lee,

All the 'member' family functions (member, vl-member-if, vl-member-if-not) return a list (or nil).
IMO, your '_member'  routine which return a boolean (T or nil) should have been named 'IsMember' or something like this.

Anyway, the 'vl-member-if' built-in function allows the same behavior as 'member' with a fuzz factor:

Code: [Select]
`(defun common (l1 l2 fuzz)  (vl-remove-if-not    (function      (lambda (x) (vl-member-if   (function (lambda (y) (equal x y fuzz)))   l2 )      )    )    l1  ))`
Code: [Select]
`(defun common (l1 l2 fuzz)  (if l1    (if (vl-member-if (function (lambda (x) (equal (car l1) x fuzz))) l2)      (cons (car l1) (common (cdr l1) l2 fuzz))      (common (cdr l1) l2 fuzz)    )  ))`
Speaking English as a French Frog

#### Lee Mac

• Seagull
• Posts: 12912
• London, England
##### Re: nested "FOR NEXT"
« Reply #29 on: November 01, 2010, 11:36:10 AM »
I agree - thanks Gile!

Can't believe I overlooked vl-member-if ...