Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

What are the advantages of having a localized functions in a routine?

16 REPLIES 16
SOLVED
Reply
Message 1 of 17
ShricharanaB
983 Views, 16 Replies

What are the advantages of having a localized functions in a routine?

Hi, 

 

What are the advantages and drawbacks of localizing a function to the main lisp routine?

 

Ex: 

(defun c:DoSomething ( / DoAnotherThing)

          (defun DoAnotherThing ( getsomething )

              (+ getsomething 1)

              )

(setq somevalue (getint "\nEnter number: "))

(princ (DoAnotherThing somevalue))
(princ)
)
16 REPLIES 16
Message 2 of 17
_gile
in reply to: ShricharanaB

Hi,

Exactly the same as for localized variables (because LISP has First-class Functions).

From the AutoLISP documentation:

"Global variables are accessible by any AutoLISP program that is loaded into a drawing, while local variables are only available within a specific function"



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 17
ShricharanaB
in reply to: ShricharanaB

Hi, 

Thank you for clearing that up. 

Message 4 of 17
Moshe-A
in reply to: ShricharanaB

@ShricharanaB hi,

 

also check this, just 2 weeks ago >> here << 

 

moshe

 

Message 5 of 17
ShricharanaB
in reply to: ShricharanaB

Hi, Thank you.

 

So does it makes sense to localize all the functions which I intend to use only in this LISP routine?

Every time higher function is run, the lisp will have to execute more code to define the functions first but it saves a little bit of memory.  

Message 6 of 17
Moshe-A
in reply to: ShricharanaB

@ShricharanaB ,

 

Yes Yes Yes  😀

 

The personal Computers for 2 decades are so fast, the RAM memory is fast and large so the SSD\Flash hard drives that even inefficient AutoLISP programs runs quickly. remember we are talking on AutoLISP programs\functions that are in general very small. and yes i am very aware, there are out there some Real BIG beautiful programs but also they can not complain on AutoLISP performance 😀

 

Moshe

 

Message 7 of 17
john.kaulB9QW2
in reply to: _gile

If I can piggy-back on what gile said (gile is correct but I thought I would add)...

 

Would it help if I defined your `doanotherthing` function like this (`defun` is essentially syntactic sugar):

 

(defun c:DoSomething ( / DoAnotherThing somevalue)
  (set 'DoAnotherThing 
       (lambda ( getsomething )
         (+ getsomething 1)) )
  (setq somevalue (getint "\nEnter number: "))
  (princ (DoAnotherThing somevalue))
 (princ)
)

 

 

And to possibly add to the `scope` sidebar:

 

(defun c:DoSomething ( / DoAnotherThing somevalue)
  (set 'DoAnotherThing 
       (lambda ( )
         (+ somevalue 1)) )
  (setq somevalue (getint "\nEnter number: "))
  (princ (DoAnotherThing))
 (princ)
)

 

 

another swamper
Message 8 of 17
Moshe-A
in reply to: john.kaulB9QW2

@john.kaulB9QW2  hi,

 

if you use (set) function [instead of (setq)], this is known in lisp as quote variable and to evaluate a quoted variable we use (eval) function or (vl-symbol-value), quoted variables has unique property, they can be pass to function by reference. (e.g their value is return back to the caller)

 

 Moshe

 

Message 9 of 17
john.kaulB9QW2
in reply to: Moshe-A

I'm not sure I understand the statement/(?question?).

Granted my lisp is extremely rusty, but I don't think lisp has pass-by-reference.
another swamper
Message 10 of 17
Moshe-A
in reply to: john.kaulB9QW2

@john.kaulB9QW2  hi,

 

i'm speachless 😀

 

(defun prtm (lst / a)
 (foreach a lst (princ a))
)

(defun NextThing (Qa Qb ;| qouted arguments |;  / c d)
  
 (foreach Qsym (list Qa Qb)
  (prtm (list "\nSymbol Name: " (vl-symbol-name Qsym) " Symbol value) " (vl-symbol-value Qsym)))
 )

 (setq c (vl-symbol-value Qa) d (vl-symbol-value Qb))
 (set Qa (+ d c)) ; addition,     indirect assignment
 (set Qb (- d c)) ; subtraction,  indirect assignment

 (list c d (vl-symbol-value Qa) (vl-symbol-value Qb)) 
); NextThing


(defun FirstThing (/ a b lst)
 (setq a 10 b 15)

 (setq lst (NextThing 'a 'b))

 ; after retrun from (NextThing) these are the values
 (prtm (list "\n" a " " b)) 

 (terpri) 
 (princ lst) ; (NextThing) returns
 (princ)
) ; FirstThing
Message 11 of 17
john.kaulB9QW2
in reply to: Moshe-A

Hi Moshe-A.

I'm sorry you're speechless because I'm a bit confused by your sidebar. -i.e. what are you trying to say? I do see the comment in your code sample that says "indirect assignment"; this must mean you are trying to demonstrate your `pass-by-ref' comment which could be a nice tutorial for you to write. However, I do not think this demonstrates "pass-by-ref" because that essentially means you pass the memory address (-i.e. pointers). However, the demo is interesting because you must be showing that you have access to the orig variable which means something is happening at the compiler level. Cool stuff, and I wish I wasn't so busy so I could actually play along.

 

FYI (10,000 foot level overview):
Pass-by-ref: pass the address, the orig variable can be modified.
Pass-by-value: pass a copy, the orig variable can NOT be modified.

another swamper
Message 12 of 17
Moshe-A
in reply to: john.kaulB9QW2

@john.kaulB9QW2 

 


@john.kaulB9QW2 wrote:

Hi Moshe-A.

I'm sorry you're speechless because I'm a bit confused by your sidebar. -i.e. what are you trying to say? I do see the comment in your code sample that says "indirect assignment"; this must mean you are trying to demonstrate your `pass-by-ref' comment which could be a nice tutorial for you to write. However, I do not think this demonstrates "pass-by-ref" because that essentially means you pass the memory address (-i.e. pointers). However, the demo is interesting because you must be showing that you have access to the orig variable which means something is happening at the compiler level. Cool stuff, and I wish I wasn't so busy so I could actually play along.

 

FYI (10,000 foot level overview):
Pass-by-ref: pass the address, the orig variable can be modified.
Pass-by-value: pass a copy, the orig variable can NOT be modified.


You can call it whatever you like "by pointer" "by reference", remember that this is not C\C++ and there is no type pointer. maybe the compiler is passing a pointers we do not know that for sure. for me the bottom line is that i can pass arguments and have them changed outside and have that values back to the caller + having the return value of the function.

 

Moshe

 

Message 13 of 17
john.kaulB9QW2
in reply to: Moshe-A

Sometimes there are too many terms to keep track of (my brain just canno't keep track of them all) but I think this would be better classified as Lexical scope. ...Let me know if I'm right or wrong.
another swamper
Message 14 of 17
Moshe-A
in reply to: john.kaulB9QW2

have strong feeling that you do not need my view on that 🤔

Message 15 of 17
john.uhden
in reply to: john.kaulB9QW2

This is one of my favorites...

Command: (defun swap (a b)(mapcar 'set (list b a)(mapcar 'eval (list a b))))
SWAP

Command: (setq x 1 y 2)

2

Command: (swap 'x 'y) ; note the symbol names must be quoted
(1 2)

Command: !x 2

Command: !y 1

John F. Uhden

Message 16 of 17
_gile
in reply to: john.uhden

The fact that @john.kaulB9QW2  used the `set` function (instead of `setq`) in his example shifted the discussion to the use of the `set` function.
I don't think I'm betraying his point when he says that "`defun` is essentially syntactic sugar" with this simple example:

(defun sqr (x) (* x x))


could also be written :

(setq sqr (lambda (x) (* x x)))

 

And any function (native or defined with `defun`) can be set to `nil` with setq.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 17 of 17
john.kaulB9QW2
in reply to: _gile

Precisely what gile said.

 

I apologize for using SET instead of SETQ (diverting the discussion). I'm still a bit confused to why SET--vs SETQ--would divert the discussion but I was only trying to back up the statement that Lisp is a "first class language".  So, in short, Gile is right, ignore me. Sorry, gile. 🙂

 

Meaning:

"... In languages with first-class functions, the names of functions do not have any special status; they are treated like ordinary variables with a function type. ..."

 

another swamper

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Autodesk Design & Make Report