Author Topic: nested "FOR NEXT"  (Read 6441 times)

0 Members and 1 Guest are viewing this topic.

Robert98

  • Guest
nested "FOR NEXT"
« on: October 26, 2010, 03:09:03 PM »
Hi dear members
I want make a nested "FOR NEXT" functions in autolisp , I see in autolisp reference that vlisp have a vlax-for , but I'm beginner and don't want use of vlisp objective properties and methods now ,so I used Allan Wise algorithm .
In addition I know that my example is very simple and may be there are very simple solutions , but my purpose is just practice on autolisp programming , if may be anybody read my codes and tell me my blunders .

thanks

codes :

Code: [Select]
(defun for ( initializer condition indexer body_code / )
  ;by Allan Wise in http://www.draftsperson.net/index.php?title=AutoLISP_Lesson_8_-_LOOPING
(eval initializer)
(while (eval condition)
(foreach n body_code (eval n))
(eval indexer))
);end defun for

;===============

(defun pred (/ A B)
   (if (/=  A B) T nil)
  );end of defun predictio

;===============

(defun REMOVE-IF-NOT (pred lst)        ; by Vladimir Nesterowsky
    (apply 'append
           (mapcar '(lambda (e)
                    (if (apply pred (list e)) (list e))) lst)))

;===============

  (defun C:MY_TEST (lis1
   lis2
   /
   x
   y)

;lis1 and lis2 are list and x , y any paeticular members of lists
   (setq lis1 '(10 12 14 15 13)
 lis2 '(74 1114 12 13 15 18)
   ) ;example lists
   (for
     '(setq x 0)
     '(<= x (length lis1))
     '(setq x (1+ x))
     '((setq A (nth x lis1)))


     (for
'(setq y 0)
'(<= y (length lis2))
'(setq y (1+ y))
'((setq B (nth Y lis2)))
(if (pred x y)
 T
 (progn
   (REMOVE-IF-NOT pred lis1)
   (REMOVE-IF-NOT pred lis2)
 ) ;end of progn
) ;end of if

     ) ;end for y

   ) ;end for x
   (princ lis1 "\n")
   (princ lis2)
   (princ)

  ) ;end of MY_TEST defun



please just in autolisp format .! :-(

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: nested "FOR NEXT"
« Reply #1 on: October 26, 2010, 03:43:32 PM »
Some quick remarks:
The for function requires 4 arguments and you supply 5.
The pred function requires no arguments and you supply 2.
Your command function C:MY_TEST requires 2 arguments. A command function normally doesn't have any arguments.

In general:
You're using chunks of quite sophisticated code without completely understanding what you are doing.
It would help if you explain what you want to accomplish.

<EDIT: typo>
« Last Edit: October 27, 2010, 05:57:41 AM by roy_043 »

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: nested "FOR NEXT"
« Reply #2 on: October 26, 2010, 04:13:50 PM »
1+

Before trying to use this 'for' routine learn how work built-in functions.
AutoLISP provides iterative functions as: repeat, while and functions to deal with items in lists: foreach, mapcar.

I know about 'for' statement in other languages and had never need to implement (or use) such routine in AutoLISP.
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12910
  • London, England
Re: nested "FOR NEXT"
« Reply #3 on: October 26, 2010, 04:18:32 PM »
1+

Before trying to use this 'for' routine learn how work built-in functions.
AutoLISP provides iterative functions as: repeat, while and functions to deal with items in lists: foreach, mapcar.

I know about 'for' statement in other languages and had never need to implement (or use) such routine in AutoLISP.

I definitely agree with this ^^

Being a high level language, AutoLISP provides many constructs that other languages don't provide - I've never had need to create control statements from other languages.

Lee

JohnK

  • Administrator
  • Seagull
  • Posts: 10623
Re: nested "FOR NEXT"
« Reply #4 on: October 26, 2010, 04:37:22 PM »
This sounds to me like a learning exercise guys (the OP gave a link to a lesson).

Robert98,
Take a quick look at this procedure and see if you can follow what it is doing.

Code: [Select]
(defun copy (x)
  (if (atom x)
    x
    (cons (copy (car x))
 (copy (cdr x))
    )
  )
)
;; Use like this:

(copy '(0 1 2 3 4 5 6 7 8 9))

Then take a look at this:
Code: [Select]
(defun map-car (process aList)
  (if (null aList)
    nil
    (cons (process (car aList))
 (map-car process (cdr aList))
    )
  )
)
;; Use like this:

(map-car (lambda (x) (1+ x)) '(0 1 2 3 4 5 6 7 8 9))


« Last Edit: October 26, 2010, 04:42:52 PM by Se7en »
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: nested "FOR NEXT"
« Reply #5 on: October 26, 2010, 04:43:55 PM »
Engineering Technologist CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com http://cadanalyst.slack.com http://linkedin.com/in/cadanalyst

JohnK

  • Administrator
  • Seagull
  • Posts: 10623
Re: nested "FOR NEXT"
« Reply #6 on: October 26, 2010, 05:11:15 PM »
wut cha thinking MP?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: nested "FOR NEXT"
« Reply #7 on: October 26, 2010, 09:58:26 PM »
Robert98, what are you really trying to achieve? Based upon your use of the code harvested from the link you provided I must deduce you are in a little over your head, so let's step back and understand what it is you want to learn and/or achieve.

Without the benefit of the above it almost appears as if you are trying to construct an intersection function, that is, determine the list elements common to two lists (like set theory intersection).

If so AND you don't wish to use vlisp functions this unoptimized code might illustrate:

Code: [Select]
(defun venn_inters ( list1 list2 / result )

    (foreach element list1
        (if (member element list2)
            (setq result (cons element result))
        )            
    )
    
    (reverse result)
    
)

example:

Code: [Select]
(venn_inters
    '(10 12 14 15 13)
    '(74 1114 12 13 15 18)
)

returns (12 15 13)

As such, nested foreach loops are not required, but I may be misinterpreting what you want to achieve. Also note that they are many ways to return the intersection of two lists, this was simple one using foreach.

An aside, while the do and for code provided in the link in your first post may have some mild academic value, from a practical stand point, i.e. what is used in typical day to day programming, they have little value for the majority of programmers imo, certainly not n00bz (no offense) and accordingly suggest you use tutorials that are more interested in helping the student new to lisp establish a solid knowledge base, rather than illuminate the teacher's prowess.
Engineering Technologist CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com http://cadanalyst.slack.com http://linkedin.com/in/cadanalyst

JohnK

  • Administrator
  • Seagull
  • Posts: 10623
Re: nested "FOR NEXT"
« Reply #8 on: October 27, 2010, 12:01:46 AM »
Yeah i was (must have been) way off. I was thinking/going a different route. Sorry.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Robert98

  • Guest
Re: nested "FOR NEXT"
« Reply #9 on: October 27, 2010, 09:07:40 AM »
Some quick remarks:
The for function requires 4 arguments and you supply 5.
The pred function requires no arguments and you supply 2.
Your command function C:MY_TEST requires 2 arguments. A command function normally doesn't have any arguments.

In general:
You're using chunks of quite sophisticated code without completely understanding what you are doing.
It would help if you explain what you want to accomplish.

<EDIT: typo>
HI ROY
These are very useful tips for me and  I'll use them , but about "completely understanding what you are doing" I said to you that :
I have read that some lisp codes can call itself like :
codes:
Code: [Select]
(defun fact (n)
   (if (<= n 0)
      1
      (* n (fact
      (- n 1))))

So I wanted test it by Allan Wise for functions, and now I believe that lisp has it's limitations and capabilities .
Thanks for your help

Robert98

  • Guest
Re: nested "FOR NEXT"
« Reply #10 on: October 27, 2010, 09:18:49 AM »
This sounds to me like a learning exercise guys (the OP gave a link to a lesson).

Robert98,
Take a quick look at this procedure and see if you can follow what it is doing.

Code: [Select]
(defun copy (x)
  (if (atom x)
    x
    (cons (copy (car x))
 (copy (cdr x))
    )
  )
)
;; Use like this:

(copy '(0 1 2 3 4 5 6 7 8 9))

Then take a look at this:
Code: [Select]
(defun map-car (process aList)
  (if (null aList)
    nil
    (cons (process (car aList))
 (map-car process (cdr aList))
    )
  )
)
;; Use like this:

(map-car (lambda (x) (1+ x)) '(0 1 2 3 4 5 6 7 8 9))
HI Se7en
First thanks for your answer
I read your codes and I still have not used it , but I think it works on a single list not two , so I wrote to you later .
thanks and have good times


HI Se7en
I worked with your functions , the map-car function add 1 to all members of a list , but copy function return me :
_$ (copy '(0 1 2 3 4 5 6 7 8 9))
(0 1 2 3 4 5 6 7 8 9)
_$

I don't understand relationship between car and cdr whit this function , please give me another example.
thanks for your help
 :|
« Last Edit: October 27, 2010, 01:20:12 PM by Robert98 »

Robert98

  • Guest
Re: nested "FOR NEXT"
« Reply #11 on: October 27, 2010, 09:27:07 AM »
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 application 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 isn't equal , how I can work with them simultaneously .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 contour , now I want filter coordinates of text and put it in list (for ample A) and then make a list of pline nodes , now suppose I want compare two list and remove undesired element and Finlay 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
correspond 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 topographic 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 simultaneously that don't have equal members (perhaps equal) 3- for this aim , I must start with ....?
sorry for my bad English skill.
thanks for your attention
« Last Edit: October 27, 2010, 01:12:01 PM by Robert98 »

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: nested "FOR NEXT"
« Reply #12 on: October 27, 2010, 09:30:09 AM »
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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10623
Re: nested "FOR NEXT"
« Reply #13 on: October 27, 2010, 09:51:19 AM »
*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.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: nested "FOR NEXT"
« Reply #14 on: October 27, 2010, 10:49:00 AM »
Engineering Technologist CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com http://cadanalyst.slack.com http://linkedin.com/in/cadanalyst