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)
)
Solved! Go to Solution.
Solved by _gile. Go to Solution.
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"
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.
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
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)
)
@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
@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
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.
@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
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
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.
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. ..."
Can't find what you're looking for? Ask the community or share your knowledge.