Defun limitation with symbols

Defun limitation with symbols

serag.hassouna
Advocate Advocate
1,064 Views
6 Replies
Message 1 of 7

Defun limitation with symbols

serag.hassouna
Advocate
Advocate

Is it possible to supply a customized symbol name to defun function?

In the ordinary cases, the programmer must supply "manually" the function names for every function definition, i.e, if you have e.g. 100 function definitions you must supply 100 different names & "MANUALLY" 100 times.

Inside the documentation the type of the supplied name is SYMBOL, however, a simple function definition like

(defun (read "myfunc") (x y) (+ x y))

fails, regardless that (type (read "myfunc")) returns SYM.
..................
I shall rephrase the question to be: "Is it possible to create customized function definitions without writing each function definition one by one?"

0 Likes
Accepted solutions (1)
1,065 Views
6 Replies
Replies (6)
Message 2 of 7

CodeDing
Advisor
Advisor
Accepted solution

@serag.hassouna ,

 

I may not be able to answer your concerns about why your symbol doesn't work as you expect, but this may the direction you are trying to go? YES, it's possible to create functions via a function.

(defun c:CFUN ( / fList f i n1 n2)
;Create FUNction
(setq fList '("fun1" "fun2" "fun3"))
;create function definitions
(foreach f fList
  (eval (read (strcat "(defun " f " (x y / ) (+ x y))")))
);foreach
;test for functions
(setq n1 0 n2 10)
(foreach i fList
  (princ "\n")
  (princ (eval (read (strcat "(" i " " (itoa n1) " " (itoa n2) ")"))))
  (setq n1 (+ 10 n1) n2 (+ 10 n2))
);foreach
(prompt "\nComplete...")
(princ)
);defun

Best,

~DD

0 Likes
Message 3 of 7

serag.hassouna
Advocate
Advocate

Thanks @CodeDing , that's very insightful.
I was toying with eval & quote before asking, e.g.

(type (quote (defun myfunc (x y) (+ x y))))

returns LIST
that's the same result for

(type (read "(defun myfunc (x y) (+ x y))"))

they give the same behavior in how to make the function definition come to life.

(setq func_with_quote (quote (defun myfunc (x y) (+ x y))))

(setq func_with_read (read "(defun myfunc2 (x y) (- x y))"))

((eval (eval func_with_quote)) 5 3) ;returns 8

((eval (eval func_with_read)) 5 3) ;returns 2

applying

(type (eval func_with_quote)) ;returns USUBR

(type (eval func_with_read)) ;returns USUBR

which means that eval works on both types

  1. LIST: for ordinary expressions
  2. USUBR: the returned value from defun function if the function definition is successful. 

However, quote returns a STATIC expression, which can't - as far as I know - be modified and customized internally, unlike strings, that makes "read" function the best choice.

..............
Quoting from the documentation about the behaviour of quote & read.

  1. READReturns the first list or atom obtained from a string.
  2. QUOTEReturns an expression without evaluating it

..............
Again, I'm really thankful to you.

0 Likes
Message 4 of 7

Sea-Haven
Mentor
Mentor
You could simply write a lisp file and load it. You can load lisp files so long as thay do not take control eg (if (not AH:getvalsm)(load "Multi Getvals.lsp")) this loads a defun.
0 Likes
Message 5 of 7

serag.hassouna
Advocate
Advocate

To Recap

  1. The first question has no answer yet.
    Which is about why defun function doesn't accept symbols that are results of other evaluation, and only accepts plain symbols.
  2. Autolisp STATIC expressions, when not evaluated yet, are of type LIST. (the relation between lists and expressions)
    Practically speaking, read function transforms a string to its equivalent non-evaluated STATIC expression.
0 Likes
Message 6 of 7

serag.hassouna
Advocate
Advocate

I appreciate your response @Sea-Haven , what you aim to tell us is that you can import function definitions written in any other lisp file to the currently in-use file.
For example, In python, function definitions and variable values can be defined in a file (called "module") and imported to the currently in-use file (aka. module).
& the function to accomplish that in AutoLISP is load, also, you inspect before loading the other lisp file if the needed function is really defined or not.
.....................
To add some context about why I'm exploring function definitions, TBH it's related to how python treats functions and function definitions.
I knew before that in python you can treat functions as objects (aka. first-class objects) which means that you can supply functions as arguments, there's an already asked question about this feature in AutoLISP.
But python supports mainly OOP and procedural styles, and AutoLISP supports mainly functional programming style.
Function definition in languages like python, C, C# uses syntax based on "keywords", and keywords are elements of statements, so mainly, a function definition in these languages is a statement (compound statement).
On the other hand, function definition in AutoLISP is also accomplished via a function.
My question is about what you can say that's sort of an inverse image to the "first-class object" feature, you can ask yourself, if functions can be passed as arguments in AutoLISP and python, can we (inversely) customize functions as we can reassign variables.
.......
I hope you understand the background of this question.

0 Likes
Message 7 of 7

Sea-Haven
Mentor
Mentor

 serag.hassouna What I was getting at is yes you can read a lisp from a lisp but rather than do eval write the lisp to be loaded from the calling lisp. This way it can be different every time.

0 Likes