I can't find it by searching, what is define* in guile? You can find it for instance in this answer https://stackoverflow.com/a/24101699/387194
You will find it in the documentation here: Creating advanced argument handling procedures.
6.10.4.1 lambda* and define*.
lambda* is like lambda, except with some extensions to allow optional
and keyword arguments.
library syntax: lambda* ([var…]
[#:optional vardef…]
[#:key vardef… [#:allow-other-keys]]
[#:rest var | . var])
body1 body2 …
Optional and keyword arguments can also have default values to take
when not present in a call, by giving a two-element list of variable
name and expression. For example in
(define* (frob foo #:optional (bar 42) #:key (baz 73))
(list foo bar baz))
foo is a fixed argument, bar is an optional argument with default
value 42, and baz is a keyword argument with default value 73. Default
value expressions are not evaluated unless they are needed, and until
the procedure is called.
Normally it’s an error if a call has keywords other than those
specified by #:key, but adding #:allow-other-keys to the definition
(after the keyword argument declarations) will ignore unknown
keywords.
From:
https://www.gnu.org/software/guile/docs/master/guile.html/lambda_002a-and-define_002a.html#lambda_002a-and-define_002a
In the Scheme standard define* is not defined but the naming convention dictates that any symbol that ends in an asterix will provide very similar operation as the symbol without.
In the standard you have let that binds variables and let* which also binds variables but one at a time so that the created variables are available to the next bindings.
There are SRFIs that are a standard way of extending Scheme. Implementations implement many of the SRFIs native and those who don't can in many cases work with just downloading the reference implementation. SRFI-89 implements define* and lambda* and they provide Scheme with optional positional arguments. Looking at Guile's SRFI support SRFI-89 is not listed but the SRFI-89 itself mentions that Guile has them except it uses the notation #:key instead of #!key, thus not portable.
It's common for R5RS implementations to have more global bindings than the standard. If it's not a part of a SRFI you will be locked in by using such extensions.
Related
Python has such methods as __add__, __mul__, __cmp__ and so on (called magic methods), which are used as a class methods and can give a different meaning to adding(+), multiplying(*), comparing(==), ... two instances of a class. My question is do other languages have a similar method? I'm familiar with Java, C++, ruby and PHP, but never came across such a thing. I know all four have a constructor method which corresponds to __init__, but what about other magic methods?
I tried googling "Magic methods in other programming languages" but nothing related showed up, probably they got different names on different languages.
In general, having too much "magic" in a language is a sign of bad language design. Maybe that is why there are not many languages which have magic methods?
Magic like this creates a two-class system: the language designer can add new magic methods to the language, but the programmer is restricted to only use the methods that the High Priest Of Language Design allows them to. In general, it should be possible for the programmer to do as much possible without requiring to change the language specification.
For example, in Scala, +, -, *, /, ==, !=, <, >, <=, >=, ::, |, &, ||, &&, **, ^, +=, -=, *=, /=, and so on and so forth, are simply legal identifiers. So, if you want to implement your own version of multiplication for your own objects, you just write a method named *. This is just a boring old standard method, there is absolutely nothing "magic" about it.
Conversely, any method can be called using operator notation, i.e. without a dot. And any method that takes exactly one argument can be called without parentheses in operator notation.
This does not only apply to methods. Also, any type constructor with exactly two type arguments can be used in infix notation, so if I have
class ↔️[A, B]
I can do
class Foo extends (String ↔️ Int)
which is the same as
class Foo extends ↔️[String, Int]
Well … I kinda lied: there is some syntactic sugar in Scala:
foo() is translated to foo.apply() if there is no method named foo in scope. This allows you to effectively overload the function call operator.
foo.bar = baz is translated to foo.bar_=(baz). This allows you to effectively overload property assignment. (This is how you write setters in Scala.)
foo(bar) = baz is translated to foo.update(bar, baz). This allows you to effectively overload index assignment. (This is how you write array or dictionary access in Scala, for example).
!foo (and a couple of others) are translated to foo.unary_!.
foo += bar will try to call the += method of foo, i.e. it is equivalent to foo.+=(bar). But if this fails and foo is a valid lvalue, and foo has a method named +, then Scala will also try foo = foo + bar instead.
Also, precedence, associativity, and fixity are fixed in Scala: they are determined by the first character of the method name. I.e. all methods starting with * have the same precedence, all methods starting with - have the same precedence, and so on.
Haskell goes a step further: there is no fundamental difference between functions and operators. Every function can be used in function call notation and in operator notation. The only difference is lexical: if the function name consists of operator characters, then when I want to use it in function call notation, I have to wrap it in parentheses. OTOH, if the function name consists of alphanumeric characters and I want to use it in operator notation, I need to wrap it in backticks. So, the following are equivalent:
a + b
(+) a b
a `plus` b
plus a b
For operator usage of functions, you can freely define the fixity, associativity, and precedence, e.g.:
infixr 15 <!==!>
In Ruby, there is a pre-defined set of operators that has corresponding methods, e.g.:
def +(other)
plus(other)
end
In C++ operator overloading is what your are looking for.
Java has no native support for operator overloading (Reference).
C has no operator overloading (Reference). Thus, a lot of add, mult and so on functions are written. Often those are macros, because then they can be used for different types. IMHO this is why I like C++ better.
#Alex gave reference to a nice overview of operator overlaoding.
Is there a term for this technique? One prominent example is the WinAPI: SendMessage( hwnd, msg, info1, info2 ) where parameters #3 and #4 only make sense per msg (which also means there are cases when only one or none of those two parameters are needed). See MSDN.
Rephrased: having an all-purpose function that always accepts multiple arguments, but interpreting them depends on a previous argument. I don't want to talk about open arrays, open arguments, typeless arguments... I know all that. That's not what I'm asking - I want to have the term for this type of functions (any maybe also how unspecific parameters are called).
This is not about casts or passing by reference - the parameter types are always the same. Other example: calculate( char operation, int a, int b ) which is then used as
calculate( '+', 2, 5 ) (parameters #2 and #3 are summands)
calculate( '/', 4, 2 ) (parameter #2 is the divident and parameter #3 is the divisor)
calculate( '!', 3, 0 ) (parameter #2 is the factorial and parameter #3 is unused)
In all these cases the data type is always the same and never casted. But the meaning of parameters #2 and #3 differ per parameter #1. And since this is the case it is difficult to give those parameters a meaningful name. Of course the function itself most likely uses a switch(), but that is not subject to my question. How are parameters #2 and #3 called, where a distinct name cannot be found, but data types are always the same?
The fact the msg argument "changes" the parameters is through a simple switch statement. Each "msg" in the switch knows the parameters(with type) needed and casts them appropriately.
This "technique" is called passing by reference, or passing by address. The latter is usually used for method pointers.
There is no Special name if that is what you are asking. It is a regular function, method or procedure.
The referenced Function is a Win32 API Function, which may be referred to as a "Windows function call."
This is an example of a static Parameter and multiple Dynamic parameters.
The static is the "msg" and the dynamic is described as the following:
These parameters are generic pointers. Passed by reference. They can point to any data type or no value, ie null pointer. It is up to the sender to lock the memory in place, and the receiving method to interpret the pointer correctly (through pointer casts).
This is an example of typeless argument passing. The only thing passed is a memory address. It is dangerous since the types passed must be agreed upon ahead of time(by convention and not contract as with a typed language construct) and must match on both sides of the call.
This was common before C++, in the C days, we only had C structs to pass around. Leading to many General Fault Protection errors. Since then, typed interfaces mostly have replaced the generic equivalents through libraries. But the underlying Win32 methods remain the same. The main substantial change since its' inception is the acceptance of 64-bit pointers.
Although not widely supported, what you are referring to would be a dependently typed function (or dependently typed parameters).
To quote wikipedia on dependent types
A "pair of integers" is a type. A "pair of integers where the second is greater than the first" is a dependent type because of the dependence on the value.
The parameters could have a type that depends on a value. The type of info1 depends on the value msg as does info2.
In order to make this approach work in a language without dependent types, the dependent parameters are given a very generic type that is only refined later on when more information is available. When the type of msg becomes known (at runtime) only then is are the types of info1 and info2 assumed. Even though the language doesn't allow you to express this dependency, I would still call the approach a dependently type one.
Let's look at http://www.ecma-international.org/ecma-262/#sec-expressions
As you can see:
IdentifierReference[Yield, Await]:
Identifier
[~Yield]yield
[~Await]await
BindingIdentifier[Yield, Await]:
Identifier
[~Yield]yield
[~Await]await
Identifier:
IdentifierName but not ReservedWord
Both Identifiers (Binding and Reference) contains the same thing. What's the point of this? What are they different?
The point is that they occur in different contexts and have different algorithms related to them.
An IdentifierReference is a variable name that is used in an expression, getting evaluated with ResolveBinding to a reference
A BindingIdentifier is a variable name that is used to create a binding - in variable and function declarations, in parameters, in destructuring, in catch clauses, in for clauses, etc.
In their EarlyErrors, an IdentifierReference may refer to eval or argument, but a BindingIdentifier creating a binding for them is a syntax error (in strict mode). The only algorithm that they share is StringValue. An IdentifierReference has a IsValidSimpleAssignmentTarget check and an Evaluation, while a BindingIdentifier has BoundNames and a BindingInitialisation procedure.
Is it possible to remove the last element from a tuple in typesafe manner for arbitrary arity?
I want something like this:
[A,B,C] abc = [a,b,c];
[A,B] ab = removeLast(abc);
No, unfortunately it's not possible, the reason being that a tuple type is represented within the type system as a linked list of instantiations of Tuple, but the type system can't express loops or recursion within the signature of a function. (And having loops/recursion would almost certainly make the type system undecidable.)
One way we could, in principle, solve this in future would be to have a built-in primitive type function that evaluates the last element type of a tuple type.
By "primitive" type function, I mean a type function that can't be written in the language itself, but is instead provided as a built-in by the compiler.
Ceylon doesn't currently have any of these sorts of primitive type functions, but there are a couple of other similar problems which could be solved in this manner.
My question is Python specific (3.4.3).
My question is specific to Built-In functions only.
It is clear to me the difference between a keyword (reserved word) and an identifier (user-defined variable).
See: https://en.wikipedia.org/wiki/Reserved_word
Likewise, I understand the basic meaning of the terminology 'function'
See : https://en.wikipedia.org/wiki/Functional_programming \
and
http://www.learnpython.org/en/Functions
However, I am having difficulty understanding the difference between Built-in functions and keywords; such as 'if' and 'for'.
https://docs.python.org/3/library/functions.html#built-in-funcs
What is the difference between the two? Keyword and Function.
Is the Keyword 'if' not simply a built in function? If so, why does it not appear in the official list of Built-In functions in the Python documentation?
https://docs.python.org/3/library/functions.html#built-in-funcs
It certainly behaves as a function. Is it simply because it preforms a procedure as opposed to returning a value? In which case how would you define it? As a method?
I have searched high and low on stackoverflow and I cannot seem to locate an answer.
Answers such as the two examples given below do not answer the overriding questions for me. Which are;
1) What defines a keyword as a keyword, rather than a builtIn function?
2) If keywords such as 'if' are not functions, then what are they? They are not classes etc. I understand that 'IF' is an example of a condition statement but what is the generic terminology for these keywords. The word keyword only defines the fact that it is reserved within the language, it does not define what the actual object is, i.e. function, class, method etc.
http://stackoverflow.com/questions/6054672/whats-the-difference-between-a-keyword-or-a-statement-and-a-function-call
http://stackoverflow.com/questions/155609/difference-between-a-method-and-a-function?rq=1
Keywords are those that describe the action to be performed, or specify how to interpret something (give meaning to instructions)
Functions are simply labels (for a set of instructions).
If you change function names it won't matter to Python (you can edit built in modules), but you can't relabel keywords.
You have already added tons of references to both, so I will not cite more.