Search code examples
functionif-statementautolisp

Lisp function exists or not checking


I want to check a function definition exists in a lisp program or not to decide which program block to run. The function definition is written on another file with.Net & I am working for AutoCAD. Please help.


Solution

  • There are many ways to do this, but ultimately you need to check whether the symbol corresponding to the function name holds a value (for example using the boundp function), and perhaps additionally whether such value is of SUBR, USUBR, or EXRXSUBR data type (using the type function).

    For example:

    (member (type YourFunctionName) '(subr usubr exrxsubr))
    

    In this case, if the symbol YourFunctionName is null, (type YourFunctionName) will return nil which will cause the member expression to return nil. Similarly, if the value held by the YourFunctionName symbol is anything other than a function, the member function will return nil.

    Since any non-nil value in AutoLISP is interpreted as True, the use of member will validate an if test expression, even though member does not explicitly return a boolean value.