Are there advantages in defining function arguments as keywords rather than just normal arguments? Was hoping to find the answer on http://docs.racket-lang.org/reference/define.html
Use keywords arguments when
- there are a lot of arguments
- most arguments have default arguments
For a function with, say, 10 arguments it can be difficult to remember the order of the argument. When keywords are used, then the order doesn't matter.
Related
in this code
void something () { /*something*/ }
are () separators or operators?
as i know in a function call () are operators:
something();
but in a function definition it would be a bit weird to have an operator, because operator in fact is a function and there is a function in a function definition?
can somebody explain this topic? what are the separators/punctuators exactly? they are tokens for the compiler to differentiate some part of the code? for example two statements?
/*statement1*/;
/*statement2*/;
;s are separating the statements from each other
so they are atomic syntactic elements for the compiler to understand the source code?
Depends on the context.
() in C++ can fulfill both definitions at once (operator and separator), or only one at a time.
It is an operator, since () is literally defined as the function call operator in the language spec. Since an overloaded operator is still an operator, this is independent of the number of arguments being passed to it (zero, or several).
A separator in terms of (programming) languages is usually defined as one or two tokens that separate some language features from other language features. This is the case when you pass parameters to a function when it is called, since the brackets separate the function name from the function arguments. This is not the case if no argument argument is being passed during the function call, as there is nothing to separate. In this case, () would act as an operator, but not as a separator.
I also almost forgot to mention the fact that round brackets are also used in arithmetic to denote precedence (acting as separators, not operators).
Another example of () acting as operator, but not as a separator, would be a cast.
I was looking at the source for the r_str macro in Julia, which parses r"text" into Regex("text"). The second argument is flags..., which passes flags into the regex, like i for case insensitive, and so on.
I was playing with this myself and got:
julia> macro a_str(p, flags...)
print(flags)
p
end
julia> a"abc"iii
("iii",)"abc"
So it seems that the iii is all passed in as the first flag. In that case, why is there the ... on the flags. Is it possible to pass in more than one element of flags to the macro?
When this question was originally asked, a macro expander – i.e. the function defined with the macro keyword, which is called to transform the expressions passed to a macro into a single output expression – was not a generic function, but rather an anonymous function, which were a different kind of function in Julia 0.4 and earlier. At that point, the only way to write an anonymous function signature which could work for either one or two arguments was to use a trailing varargs argument, which is why this pattern was used to define string macros. In Julia 0.5 all functions have become generic functions, including anonymous functions and macro expanders. Thus, you can now write a macro a variety of ways, including the old way of using a varargs argument after the string argument:
# old style
macro rm_str(raw, rest...)
remove = isempty(rest) ? "aeiouy" : rest[1]
replace(raw, collect(remove), "")
end
# new style with two methods
macro rm_str(raw)
replace(raw, ['a','e','i','o','u','y'], "")
end
macro rm_str(raw, remove)
replace(raw, collect(remove), "")
end
# new style with default second argument
macro rm_str(raw, remove="aeiouy")
replace(raw, collect(remove), "")
end
These all result in the same non-standard string literal behavior:
julia> rm"foo bar baz"
"f br bz"
julia> rm"foo bar baz"abc
"foo r z"
The string literal produces the string with the flagged letters stripped from it, defaulting to stripping out all the ASCII vowels ("aeiouy"). The new approach of using a second argument with a default is the easiest and clearest in this case, as it will be in many cases, but now you can use whichever approach is best for the circumstances.
With an explicit call like
#a_str("abc", "iii", "jjj")
you can pass multiple flags. But I'm not aware of a way to make this work with a"abc"ijk syntax.
I don't believe it is possible, and the documentation doesn't provide an example where that would be used. In addition, the mostly-fully-compliant JuliaParser.jl doesn't support multiple flags either. Perhaps open an PR on Julia changing that?
I know that my is used to declare a variable local to a block or file. I have always assumed that my is a keyword in Perl. But I was just told that it's actually a function. One of the proofs is that perldoc puts my under the “Functions” section, see http://perldoc.perl.org/functions/my.html.
How does a function do the job of declaring local variables?
my is not a function, it's just clumped together with functions (in perl documentation) because it works like a function.
If you look at perldoc perlfunc, it is saith,
Here are Perl's functions (including things that look like functions, like some keywords and named operators) arranged by category...
then a bit below that
Keywords related to scoping
caller, import, local, my, our, package, state, use
Specifically, note that the word “keyword” was used there instead of “function”
So that implies that you would find some non-functions (e.g. keywords) under Perl functions A-Z
Another way of saying this: if something is listed under “Functions” in perldoc, it is not necessarily a function – it can be a keyword or named operator which acts like a function.
Yes, by Perl's (very unique) definition, my is a function. The opening paragraph of perlfunc defines "function":
The functions in this section can serve as terms in an expression. They fall into two major categories: list operators and named unary operators.
my is a named operator. But it's special in two ways:
In addition to behaving like a function (that allocates a new variable and returns that variable), it has a compile-time effect.
my ... is a unary operator, but it can accept multiple arguments when parens are used.
If on the other hand you were ask if my was a function by C's definition, then no. my is not a C function. Neither is print, open, chr, etc. Everything in perlfunc is an operator; none of them are functions.
Finally, print, open and chr are far closer to a person's conception of a function than my. To be more precise, few people would consider my to be a function. It's more of a technicality than anything meaningful that it matches perfunc's definition of function.
See also:
What are perl built-in operators/functions?
Why does this [my] variable keep its value
Is it possible to use an uninitialized variable as a function argument? For an assignment, I have to use the CLOS to write a semantic network system, and my professor included a test function to test our output, and one of them specifies:
(print (def-concept Human)),
which implies passing the argument Human to the function def-concept. When running this test function, I cannot get away from the error (in Allegro CL):
Error: Attempt to take the value of the unbound variableHUMAN'.`
As this is the first function in the test, there is no initializing of any variables before this. Is there any way to get around passing an uninitialized variable as an argument of a function?
Thanks in advance.
It is not possible to use an unitialized value as a function argument in a regular Common Lisp function call. Common Lisp uses eager evaluation: argument expressions are reduced to their values before the function call takes place.
I suspect maybe you don't exactly understand the structure of the homework assignment.
If def-concept is a function which expects a value, and human is not defined, you simply cannot test that function.
Perhaps you are expected to define the variable human and then load the file containing (print (def-concept human)).
Just because there is nothing prior to that form in the same file doesn't mean that no prior evaluation is possible. Other files can be loaded before that file, or forms evaluated in the listener.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
When should I use the & to call a Perl subroutine?
In perl scripts, why is that method of invoking the function is written differently some times. I see &function and sometimes function(). Are they both the same and this is just a style that one would want to flaunt? If they are the same, why are they both available, would not one just suffice? I am guessing there is some semantic difference between the both the methods which distinguishes them from each other ... but at what kind of circumstances?
--- Since I cannot answer my own question for timeout reasons -- I am updating the answer in the section of question itself. When I get a chance to update the answer block, I will put it there.
I found the relevant text in 'Learning Perl' book..thanks for the tip though. Chapter 4: Subroutines -- Omitting the Ampersand.
I was more interested in ampersand & usage for perl functions. If a subroutine is already defined before being invoked, then subroutine can be invoked without using & while calling the function similar to invoking the builtin functions. & is also used to distinguish between the builtin functions and the user defined functions if the function to be invoked uses the same name that of one of the builtin function, provided it is defined before being invoked.
Usage of (), is merely to justify the passing of the arguments to the subroutines, while if not used, the default list of current arguments are passed in the form #_. If the arguments are specified in () for a subroutine, it is assumed to be a subroutine and is invoked even if not previously defined while parsing.
It has very specific uses:
& tells Perl to ignore the sub's prototype.
& can allow you to use the caller's #_. (&foo; with no parens or arguments).
goto &foo; and defined &foo.
Getting a reference (e.g. \&foo).
Some people (mostly beginners) use it to distinguish user subs from builtin functions, since builtin functions cannot be preceded by &.
As mentioned by #manatwork and #KeithThompson you can find information in these articles:
A general description - What's the difference between calling a function as &foo and foo()?
Subtle information about using & or not for a function call - perlsub: Perl Subroutines: Description.