TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Mark on July 12, 2005, 08:16:08 AM

Title: Looping techniques
Post by: Mark on July 12, 2005, 08:16:08 AM
Anyone want to discuss the different looping techniques, foreach, while etc.?

What are the differences between them?

When should I use 'repeat'?
When should I use 'foreach'?

etc...
Title: Looping techniques
Post by: TimSpangler on July 12, 2005, 09:13:04 AM
I would be interested in seeing the differances myself.  It would be an interesting thread.
Title: Looping techniques
Post by: whdjr on July 12, 2005, 09:26:02 AM
Are we classifying mapcar, apply, vlax-for, and vlax-map-collection as looping functions as well?
Title: Re: Looping techniques
Post by: whdjr on July 12, 2005, 09:28:29 AM
Quote from: Mark Thomas
Anyone want to discuss the different looping techniques, foreach, while etc.?

What are the differences between them?

When should I use 'repeat'?
When should I use 'foreach'?

etc...

I personally like mapcar the best (if it's considered looping); however of the two mentioned above I use repeat from time to time but I haven't used foreach in a long time.

my 2c
Title: Looping techniques
Post by: Mark on July 12, 2005, 09:35:07 AM
Quote from: whdjr
Are we classifying mapcar, apply, vlax-for, and vlax-map-collection as looping functions as well?

vlax-for definitely. I'm not sure I would consider the others as looping type functions but perhaps one of the masters will clarify this for us. :)

How about some examples?
Title: Looping techniques
Post by: whdjr on July 12, 2005, 11:28:22 AM
Well here's one for 'repeat':
Code: [Select]
(defun *ssnames* (selection_set / num lst)
  (repeat (setq num (sslength selection_set))
    (setq num (1- num)
 lst (cons (ssname selection_set num) lst)
    )
  )
  lst
)


and here's one for 'vlax-map-collection':
Code: [Select]
(defun *layout_list* (/ lst)
  (vlax-map-collection
    (vla-get-activedocument (vla-get-documents (vlax-get-acad-object)))
    '(lambda (x) (setq lst (cons x lst)))
  )
)


and here's one using 'vlax-for':
Code: [Select]
(defun *get_block_objs* (name document / blkObjlist)
  (vlax-for item (vla-get-modelspace document)
    (if (and (= (vla-get-ObjectName item) "AcDbBlockReference")
    (= (vla-get-name item) name)
)
      (setq blkObjlist (cons item blkObjlist))
    )
  )
  blkObjlist
)


Those are just a few.
Title: Looping techniques
Post by: whdjr on July 12, 2005, 11:31:19 AM
Quote from: Mark Thomas
... I'm not sure I would consider the others as looping type functions but perhaps one of the masters will clarify this for us.

While mapcar, apply, and vlax-map-collection don't act like the traditional 'loop' functions they do inspect each item or act upon each item which would imitate a loop.
Title: Looping techniques
Post by: Tom Smith on July 12, 2005, 11:53:13 AM
Of the simpler mechanisms in the original post, these can overlap in functionality, but the most natural usages would be ...

Repeat - when the number of iterations of the same operation is known, as in printing "hello world" five times. There's an implied counter.

Foreach - when the work to be done has some correspondence to the members of a known list. The list in effect serves as a counter.

While - when the work is to continue until a specific condition is met. Usually an explicit counter variable is set and incremented (with a cutoff value) or a flag variable is established to signal completion.

I wouldn't consider mapcar and apply to be "loop" control structures in quite the same sense, though their operation is constrained by a list.
Title: Looping techniques
Post by: SMadsen on July 12, 2005, 11:55:04 AM
Will, APPLY merely pushes a function onto a list of arguments and forces evaluation. Although recursive for certain functions, it's not really a loop type function. E.g. for symbol type functions, you can do a simple definition like (defun myApply (fn lst)(eval (cons fn lst))).
Title: Looping techniques
Post by: JohnK on July 12, 2005, 12:38:58 PM
Code: [Select]
(defun map (proc items)
  (if (null items)
    nil
    (cons (proc (car items))
          (map proc (cdr items)))))


Here is a perdy safe assumption of what Mapcar looks like. As you can see its a recursive procedure that terminates when it runs out of items to process. So I would almost concider Mapcar to be a loop in the word's "traditional" sense.
Title: Looping techniques
Post by: daron on July 12, 2005, 01:20:05 PM
Personally, I would use repeat when I don't feel the inclination to place a selection set in an itemized list. I've found foreach and mapcar to have similar uses with some differing results.
Title: Looping techniques
Post by: Tom Smith on July 12, 2005, 02:05:23 PM
Per Se7en, I suppose we do have to consider mapcar a sort of a loop, in that it's defined recursively, and loops pretty much as foreach does.

Recursion itself is a related topic, as a way of structuring a routine. I'd think of it as a sort of glorified "while" process.
Title: Looping techniques
Post by: whdjr on July 12, 2005, 02:25:20 PM
Quote from: Tom Smith
Per Se7en, I suppose we do have to consider mapcar a sort of a loop, in that it's defined recursively, and loops pretty much as foreach does.

Recursion itself is a related topic, as a way of structuring a routine. I'd think of it as a sort of glorified "while" process.

I don't want to argue but since we are having a discussion, I don't think the comparision to 'while' is accurate because 'while' will stop after it returns a 'nil' value whereas 'mapcar' iterates through the entire selection.

(just since we are discussing)
Title: Looping techniques
Post by: CAB on July 12, 2005, 02:41:14 PM
So we can say that mapcar is like foreach
and vl-some is like while 8)
Title: Looping techniques
Post by: Tom Smith on July 12, 2005, 03:30:45 PM
Will, I guess I wasn't clear, I didn't intend to compare mapcar with while -- I said mapcar is pretty much like a foreach in operation in that it cycles over the whole list with no bail-out condition.

I was refering to recursion in general as being like a fancy while. The only relation to mapcar being that Se7en had showed how mapcar itself could be modelled as a recursion over a list.

A recursive function might depend on an if or a cond for a termination condition, and might often pass a list as an argument, but the only real defining feature is that the function calls itself, and keeps doing so until some terminating condition is met. In most cases you can rewrite a recursive function as an iterative one using while.
Title: Looping techniques
Post by: whdjr on July 12, 2005, 03:45:28 PM
Ok, I got ya.
Title: Looping techniques
Post by: daron on July 12, 2005, 04:09:56 PM
What about vl-every? Where does that fit in?