TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: whdjr on October 07, 2004, 11:57:12 AM

Title: Indeterminate number of arguments?
Post by: whdjr on October 07, 2004, 11:57:12 AM
How would you setup a function to accept an indeterminate number of arguments?

ex:

(this_function arg1 arg2 arg3)
or
(this_function arg1 arg2)

Where this_function is the same function.
Title: Indeterminate number of arguments?
Post by: MP on October 07, 2004, 12:04:38 PM
Code: [Select]
(defun MyFunction ( ParameterList  / NumberOfParameters )
    (cond
        (   (eq 4 (setq NumberOfParameters (length ParameterList)))
            ;;  4 parameters
        )
        (   (eq 3 NumberOfParameters)
            ;;  3 parameters
        )
        ...
    )
)
Title: Indeterminate number of arguments?
Post by: Keith™ on October 07, 2004, 12:05:47 PM
in lisp you don't unless you send your arguments as a list ....

Code: [Select]

(defun myproggie ( argumentlist )
 ;;;do stuff here
)


then call it like so...

Code: [Select]

(myproggie (list arg1 arg2 arg3 ..... ))
Title: Indeterminate number of arguments?
Post by: whdjr on October 07, 2004, 12:10:58 PM
Very snazzy thinking.
I'll have to try that.

Thanks guys.
Title: Indeterminate number of arguments?
Post by: whdjr on October 07, 2004, 12:13:15 PM
I noticed in some of the express tools they call a function that could accept 2, 3, or 4 arguments, but they don't supply it as a list.

What gives?
Title: Indeterminate number of arguments?
Post by: Keith™ on October 07, 2004, 12:16:22 PM
dang MP you beat me to it..
Title: Indeterminate number of arguments?
Post by: Keith™ on October 07, 2004, 12:17:27 PM
some expresstools are written as ARX programs... (in C++) you can have a variable number of arguments to C++ programs.
Title: Indeterminate number of arguments?
Post by: MP on October 07, 2004, 12:20:36 PM
Quote from: whdjr
I noticed in some of the express tools they call a function that could accept 2, 3, or 4 arguments, but they don't supply it as a list.

What gives?

You can write functions / procedures in C++ that have a variable number of arguments. You can in VB6 as well, but some of the express tools (ET) functions you refer to are (most likely) written in ARX (C++ based).

By the way I cannot recommend the use of  ET functions to anyone doing serious development. You cannot count on them being on a given computer, and when they're not; *poof* your program is instantly lobotomized; cannot run. So my advice is to stay as far away from ET stuff as possible, despite the presence of some useful stuff. If you need 3rd party tools consider using DOSLIB.
Title: Indeterminate number of arguments?
Post by: whdjr on October 07, 2004, 12:29:33 PM
I use it mainly for reference.
If there is something I can't live without I try to track what they did and rewrite it for my use.

Thanks for the warning though.
Title: Indeterminate number of arguments?
Post by: CAB on October 07, 2004, 12:42:51 PM
Quote from: whdjr
I use it mainly for reference.
If there is something I can't live without I try to track what they did and rewrite it for my use.

Thanks for the warning though.

Here is some more reference 8)
http://theswamp.org/phpBB2/viewtopic.php?t=1922&highlight=number+arguments
Title: Indeterminate number of arguments?
Post by: MP on October 07, 2004, 01:04:01 PM
Interesting thread CAB, but I must say that in all my years programming (now sadly measured in decades) I've found very few scenarios that ultimately required a function / procedure that would accept a variable number of arguments, at least very few that couldn't be solved by re-examining the problem and solution(s).

Still, it's an interesting read and if you do have one need that is more elegantly solved going this route it's useful info; thanks.
Title: Indeterminate number of arguments?
Post by: CAB on October 07, 2004, 01:21:28 PM
Well there wasn't so much a need for this but a curiosity. Asking Why not.
And the next thing you know we are off on a 'Wild Goose Chase'.
Title: Indeterminate number of arguments?
Post by: David Bethel on October 07, 2004, 04:02:08 PM
MP,

I use a single list parameter quite often just so I can have a varable number of parameters.

Code: [Select]
(setq data '("X20" "Y28" "Z14" "L24" "R36" "B3" "S12"))

(defun my_foo (input / x y z q w l d)

  (foreach a input
      (setq dtype (strcase (substr a 1 1)))
      (cond ((= dtype "X") (setq x (atoi (substr a 2))))
            ((= dtype "Y") (setq y (atoi (substr a 2))))
;;And so on
)

;;;And then make defaults for the missing atoms

(if (not r) (setq r 36))

;;;Or simply ingore them as that they are not needed for this particular style of the model



-David
Title: Indeterminate number of arguments?
Post by: Columbia on October 08, 2004, 09:37:00 AM
In doing some serious development I have found a very large need for the use of a variable argument list.

Let me explain just a little, and maybe you can see my point (or think I'm just plain nutsoid :) )

Anyway...  I use the Active-X entity creation routines (vla-addLine, vla-addCircle, etc...) to automate my entity creation libraries.  So now what I have is a function sort of like this...

Code: [Select]

(defun Util-Add-Circle (/ activeSpaceObject centerPoint radius returnObject)
  ....
  (setq returnObject
    (vla-addCircle
      activeSpaceObject
      (vlax-3d-point centerPoint)
      radius
    )
  )
)


So now I use this function in the following manner...

Code: [Select]

(defun DoSomething (/ pt rad)
  (setq pt (getpoint "\nPick center point...")
          radius (getdistance "\nEnter radius: ")
  )
  (Util-Add-Circle pt radius)
)


But now I want to change the properties of the newly added function so I add the following lines to the DoSomething function.

Code: [Select]

(defun DoSomething (/ pt rad o)
  (setq pt (getpoint "\nPick center point...")
          radius (getdistance "\nEnter radius: ")
  )
  (setq o (Util-Add-Circle pt radius))
  (vla-put-layer o "NewLayer")
  (vla-put-Color o acRed)
)


I kept finding that cumbersome in every single drawing routine I had.  I wasn't sure that would always want to change the same number of properties in the function so I re-wrote the Util-Add-Circle routine to take a dotted pair list of properties.  That way I can have a dynamic list of properties that I want to change at runtime.  I just need to make sure that the center point and the radius arguments are always supplied.  But that's only small error trapping/checking.  But to illustrate my point just a little...

Code: [Select]


;;; function call
(Util-Add-Circle (list (cons "Center" pt) (cons "Radius" 0.3) (cons "Layer" "NewLayer") (cons "Color" acRed)))

(defun Util-Add-Circle (propList)
  (vla-addCircle
     activeSpaceObject
     (vlax-3d-object (cdr (assoc "Center" propList)))
     (cdr (assoc "Radius" propList))
  )
  (mapcar '(lambda (x) (vlax-put-property (car x) (cdr x)) propList)
)


Of course, this snippet is not really complete.  The proplist should have the "Center" and "Radius" atoms removed from the list before the mapcar is applied.  And there is not sufficient error trapping.  But I wanted to deliver here was a viable and most definitely usable way to illustrate the need for variable argument lists.  However, I'm not going to try to say that these argument lists can or even should be used every where.  But there applicable locations.

I hope I didn't confuse the issue any...
Title: Indeterminate number of arguments?
Post by: CAB on October 08, 2004, 10:59:23 AM
I like the idea.
I would add Diameter to the list & test for (or Radius Diameter)
Title: Indeterminate number of arguments?
Post by: MP on October 08, 2004, 11:06:10 AM
Some really good thoughts presented in this thread. While the use of variable number of arguments hasn't been a need in much in my work I've enjoyed reading the various justifications here; good stuff.