Autolisp user function overloading - autolisp

You can read this on AutoCAD knowledge website:
"Note: You can define multiple user functions with the same name, but have each definition accept a different number or type of arguments."
Has anybody using this feature? I tried but does not work at all.
I can call only the latest defined function.If I call like this (file::AppendFile arg1) then autocad said I give too less argument

I'm not at a computer with AutoCAD installed, so I can't check if AutoLISP works the way the documentation says it should, but I do know I've seen a workaround to pass a variable number of arguments into a function.
The trick is to pass all your arguments as a single list, and then process that list in the body of the function. For example:
(defun myFunction (argsList / path header)
(setq path (car argsList))
(setq header (cadr argsList))
(someFunction path "a" header)
)
... and then you'd call your function with (myFunction '("arg1")) or with (myFunction '("arg1" "arg2")).
Note that in my examples above I'm using the list constructor literal, so it will pass in the actual strings "arg1" and "arg2". If you want to pass in the contents of variables, you'd need to use the form (myFunction (list var1 var2)) instead, because (myfunction '(var1 var2)) would pass in the symbols 'var1 and 'var2 instead of their values.
It's a little bit ugly, but it's an option.

"Note: You can define multiple user functions with the same name, but have each definition accept a different number or type of arguments."
This is not possible in AutoLISP: the last defun expression evaluated will overwrite all previous definitions of the symbol in the namespace - hence, in your example the file:AppendFile function would require two arguments, as the second defun expression will immediately redefine the function.
The only way to supply two arguments (other than supplying a list of arguments of varying length) would be to evaluate the file:AppendFile function prior to the evaluation of the second defun expression.

Related

PL/SQL mysterious arrow operator

What is the operator "=>" we can often see in the UTL_TCP usage examples ?
As in http://www.oracle-base.com/articles/misc/ftp-from-plsql.php
l_conn := ftp.login('ftp.company.com', '21', 'ftpuser', 'ftppassword');
ftp.ascii(p_conn => l_conn);
ftp.get(p_conn => l_conn,
p_from_file => '/u01/app/oracle/test.txt',
p_to_dir => 'MY_DOCS',
p_to_file => 'test_get.txt');
ftp.logout(l_conn);
I dont understand what is the purpore of "p_conn => l_conn", since we never use p_conn anywhere.
Even closing the connection is done with ftp.logout(l_conn), not using p_conn.
All the variables used before this operator "=>" arent even defined anywhere.
Maybe it's an operator specific to the UTL_TCP package, because I never saw it being used anywhere else, and cant find it in any PL/SQL documentation, Oracle or otherwise.
It's a method to pass parameters to a PL/SQL subroutine called named notation.
For more information see the official Oracle documentation:
http://docs.oracle.com/cd/B12037_01/appdev.101/b10807/08_subs.htm#sthref1013
It's mostly used when you don't know how much parameters a procedure does expect or in which order they are expected. So you just name every parameter you want to pass with it's according value.
Excerpt from the documentation:
Positional notation. You specify the same parameters in the same order as they are declared in the procedure.
This notation is compact, but if you specify the parameters (especially literals) in the wrong order, the bug can be hard to detect. You must change your code if the procedure's parameter list changes.
Named notation. You specify the name of each parameter along with its value. An arrow (=>) serves as the association operator. The order of the parameters is not significant.
This notation is more verbose, but makes your code easier to read and maintain. You can sometimes avoid changing your code if the procedure's parameter list changes, for example if the parameters are reordered or a new optional parameter is added. Named notation is a good practice to use for any code that calls someone else's API, or defines an API for someone else to use.
Mixed notation. You specify the first parameters with positional notation, then switch to named notation for the last parameters.
You can use this notation to call procedures that have some required parameters, followed by some optional parameters.
Excerpt from documentation:
Positional Versus Named Notation for Subprogram Parameters
When calling a subprogram, you can write the actual parameters using either positional or named notation. That is, you can indicate the association between an actual and formal parameter by position or name. So, given the declarations
DECLARE
acct INTEGER;
amt REAL;
PROCEDURE credit_acct (acct_no INTEGER, amount REAL) IS ...
you can call the procedure credit_acct in four logically equivalent ways:
BEGIN
credit_acct(acct, amt); -- positional notation
credit_acct(amount => amt, acct_no => acct); -- named notation
credit_acct(acct_no => acct, amount => amt); -- named notation
credit_acct(acct, amount => amt); -- mixed notation

Erlang Looping through a list (or set) to process files

I want to create 16 directories in Erlang.
for ( create_dir("work/p" ++ A, where A is an element in a list [0, 1, ... f]) (sixteen number in hex notation).
I could of course write sixteen lines like: mkdir ("work/p0"), mkdir("work/p1") etc.
I have looked at lists:foreach. In the examples fun is used, is possible to define a function outside the loop and call it?
I am new to Erlang and used to C++ etc.
Yes, it's possible to define a (named) function outside the call to lists:foreach/2. Why would you, though? This is a case when an anonymous function is incredibly handy:
lists:foreach(fun(N) ->
file:make_dir(
filename:join("work", "p"++integer_to_list(N, 16)))
end, lists:seq(0, 15)).
The filename:join/2 call will use the appropriate directory separator to construct the string work/pN, where N is an integer in hex representation constructed using integer_to_list/2, which converts an integer to a string (list) in a given base (16).
lists:seq/2 is a friendly little function that returns the list [A,A+1,A+2,...,B-1,B] given A and B.
Note that you could just as well have used the list comprehension syntax here, but since we're applying functions to a list for the side-effects alone, I chose to stick with a foreach.
If you really want to define a separate function -- let's call it foo and assume it takes 42 arguments -- you can refer to it as fun foo/42 in your code. This expression evaluates to a function object that, like an anonymous function defined inline, can be passed to lists:foreach/2.

Exclamation operator?

I'm learning D and have seen a lot of code like this:
ushort x = to!ushort(args[1]);
I assume this casts args[1] to ushort, but what's the difference between this and cast(ushort)?
EDIT: And what other uses does the exclamation mark operator have?
In D,
to!ushort(args[1])
is shorthand for the template instantiation
to!(ushort)(args[1])
and is similar to
to<ushort>(args[1])
in languages like C++/Java/C#.
The exclamation point is to note the fact that it's not a regular argument, but a template argument.
The notation does not use angle brackets because those are ridiculously difficult to parse correctly for a compiler (they make the grammar very context-sensitive), which makes it that much more difficult to implement a correct compiler. See here for more info.
The only other use I know about is just the unary 'not' operation (e.g. false == !true)... I can't think of any other uses at the moment.
Regarding the cast:
cast(ushort) is an unchecked cast, so it won't throw an exception if the value is out of range.
to!ushort() is a checked cast, so it throws an exception if the value is out of range.
The exclamation mark here is not an operator, it is just a token part of the explicit template instantiation syntax (described in detail here).
std.conv.to (docs) is a function template for converting between arbitrary types. It is implemented entirely in the library and has no special support in the language. It has a broader and different scope compared to the cast operator.
The to template takes two type parameters; a "to" type and a "from" type, in that order. In your example, the template is explicitly instantiated with the single type argument ushort for the "to" parameter, and a second type argument string (assuming args comes from the first parameter to main) is automatically inferred from the regular function argument passed to the function (args[1]) as the "from" parameter.
The resulting function takes a string parameter and returns a ushort parsed from that string, or throws an exception if it failed. The cast operator will not attempt this kind of high-level conversion.
Note that if there is more than one explicit template parameter, or that parameter has more than one token in it (ushort is a single keyword token), you must wrap the template parameter list in parentheses:
ushort result;
result = to!(typeof(result))(args[1]);
In this example, typeof, (, result and ) are four separate tokens and the parentheses are thus required.
To answer your last question, the ! token is also used for the unary not operator, unrelated to template instantiations:
bool yes = true;
bool no = !yes; // 'no' is false
You already got two excellent answers by jA_cOp and Merhdad. I just want answer directly to the OP question (what's the difference between this and cast(ushort)?) - The difference is that cast(ushort)args[1] will not work (you cannot cast from a string to an uint just like that), while the to!(type)(param) template knows what to do with the string and how to convert it to the primitive type.

How to see docstrings and other symbol information in Common Lisp REPL?

I'm completely new to CL, and I'd like to learn how to read documentation strings and get other help information from the REPL. Something like help(symbol) in Python, or symbol? in iPython, or :t and :i in Haskell's GHCi.
So, given a symbol name, I'd like to be able to know:
what kind of value it is bound to, if any (a function, a variable, none at all)
if it is a function or a macro, then what are its positional arguments
if it has a docstring, show it
what package or file it is coming from or when it was defined
I found there is (documentation '_symbol_ '_type_), but it is not exactly what I need. I need to know the type of value the symbol is bound to ('function, 'variable, 'compiler-macro, etc.) before I can use documentation. Then it returns only the docstring, it may be missing or not sufficient to use the symbol.
For example, in Lisp, the help for mapcar is not very useful (CLisp's REPL):
> (documentation 'mapcar 'function)
NIL
I'd like to be able to see something like this instead:
>>> map?
Type: builtin_function_or_method
Base Class: <type 'builtin_function_or_method'>
String Form: <built-in function map>
Namespace: Python builtin
Docstring:
map(function, sequence[, sequence, ...]) -> list
Return a list of the results of applying the function to the items of
the argument sequence(s). If more than one sequence is given, the
function is called with an argument list consisting of the corresponding
item of each sequence, substituting None for missing values when not all
sequences have the same length. If the function is None, return a list of
the items of the sequence (or a list of tuples if more than one sequence).
As mentioned Common Lisp has standard functions: DESCRIBE, INSPECT and DOCUMENTATION. Typical Lisp IDEs also have these bound to keys and menus.
For standard Common Lisp functionality most IDEs directly link to the Common Lisp HyperSpec documentation with a keystroke.
Most IDEs also have keystrokes to show the arglist and the documentation. There is also the 'arglist on space' functionality.
LispWorks specific examples: LispWorks Argument list information and LispWorks Expressions menu
I can recommend to read the IDE manual for Slime, LispWorks Editor, Allegro CL's ELI, or whatever IDE you are using.
Regarding your question about getting the type of symbol: there is no such thing. Or, more precisely, symbols are not just names of other objects, but themselves objects of the type SYMBOL. Each symbol can have both a variable value and a function value. To check if it has a variable value, use BOUNDP, and to check for a function value FBOUNDP.

What is the definition of ":=" in vb

I came across some sample code in VB.Net which I have some experience with and kinda having a duh moment figuring out the meaning of :=.
RefreshNavigationImages(bForward:=True, startIndex:=-1)
The sig for this method is RefreshNavigationImages(boolean, int). Is this a default value if null? Like "bIsSomething ?? false"?
Tried to bing/google but they just don't like searching for operators especially if it's only 2 chars.
They are named parameters. They let you specify values for arguments in function calls by name rather than order.
The := indicates the use of named parameters. Rather than relying on the order of the parameters in the method declaration, named parameters allow you to specify the correlation of parameters to values by specifying the name of the parameter.