I used to think variable as data and function as data mappings, and they are distinct things in general sense. However, when reading some language standard (namely, Haskell), I found that the standard appears to group them together (see 2.4, and 10.5, and this question ).
So, I have a few questions regarding variable and function (in addition to the question title):
Edits:
Summary
After reading all the answers (they are really great), I realized that my doubts was due to confusion between "operator" and "function": I wrongly mixed the two things up. The correct understanding should be: an "operator" is just a symbol that refers to a "function".
Allow me to borrow the explanation from E-Lisp Intro that I found very helpful:
We can articulate another characteristic of Lisp based on what we have discussed so far—an important characteristic: a symbol, like +, is not itself the set of instructions for the computer to carry out. Instead, the symbol is used, perhaps temporarily, as a way of locating the definition or set of instructions. What we see is the name through which the instructions can be found. Names of people work the same way. I can be referred to as ‘Bob’; however, I am not the letters ‘B’, ‘o’, ‘b’ but am, or was, the consciousness consistently associated with a particular life-form. The name is not me, but it can be used to refer to me.
yes. drive will be a variable whose value is a named function with the same name. The compiler declares the variable for you, gives it a named function as the value of it -and with the same name as the variable-, and also hoists the variable. This is done by the compiler as you declare a function.
If a variable y is so related to a variable x that whenever a numerical value is assigned to x, there is a rule according to which a unique value of y is determined, then y is said to be a function of the independent variable x.
Using instanceof operator The instanceof operator is used to check the type of objects at run time. This operator returns a Boolean value(true or false). In the example below, an IF statement is used to check if the type of parameter passed to checkFunction() is of Function type or not.
Use the typeof operator to check if a function is defined, e.g. typeof myFunction === 'function' . The typeof operator returns a string that indicates the type of a value. If the function is not defined, the typeof operator returns "undefined" and doesn't throw an error. Copied!
No, a function is not a variable. A function is a value (in Haskell, Python, JavaScript, Clojure, and many other languages, but not in all languages). A variable (or symbol, identifier) can refer or be bound to a value. Therefore, a variable/symbol/identifier can refer to a function.
Your sub-questions:
IMHO, yes. There are higher-order logics in mathematics, for example. There are also common higher-order functions used in mathematics such as derivatives, integrals, and summation.
IMHO, variables aren't defined by "having a type" -- values also have types, albeit possibly in a different sense -- but rather by their ability to represent (stand in for, refer to, point to) something else.
"What is a variable?" should probably be asked as a separate question, because a precise answer is surprisingly complicated and rooted in the history of mathematics, from what I understand.
Reading your question more carefully, I think there are two separate issues here: 1) what "is" something? and 2) similarities between different things. So while you've identified similarities between variables and functions, that doesn't mean that functions "are" variables or vice versa. I'll try to illustrate this with a Java example (bear with me here!): suppose you have a Java program with two classes, Variable
and Function
, and you want to know how to create an inheritance relationship between them. My answer is that I don't think there is one -- neither one inherits from the other -- but you don't need one because you can instead create interfaces capturing the aspects that you're interested in, and then implement those interfaces for both classes.
A function is a syntactic form that looks like
\ x -> E
along with a few variations. If the expression E
has type b
and all references to x
in E
have type a
then the type of the whole expression is a -> b
.
Conversely, something like take
or (.)
is a variable. It turns out that in each of those cases the type of that variable is of the form a -> b
so that variable represents a function.
Since Haskell is pure we're guaranteed that replacing a variable with the value it represents will not change the meaning of the program.
(I'll ignore for the moment the difference between syntactic forms and values—you can blur that distinction for a long time without too much trouble.)
This notion of value freely replacing reference is the typical meaning of a variable in mathematics—though not the most core way of reflecting on it.
Generally, a variable is an unknown which may be constrained by any number of things. For instance, it could participate in a system of equations and be constrained to be a solution to that system
a = (True, a) -- invalid Haskell, but a valid concept! What is "a" here?
Haskell variables cannot be so directly defined: there are restrictions on the kinds of recursive equations a variable can participate in and these are given by the typing judgements of Haskell.
But despite me trying to drive a distinction between "typical" mathematical variables and Haskell variables, the two are FAR more similar than "variables" in other computer languages. Those might be better named "mutables" or "assignables".
Finally, what is the essence of a function? If you "have" a function either via variable reference or syntactic form then you can apply it to an argument. This causes a form of variable substitution to occur. We already stated that variables can be freely substituted when they reference somethjng though—why do we need functions?
Because the construction of a function produces a context which contained bound variables. These variables are ones that only make sense within the body of the function and fail to take any actual reference until the function is applied and the local bubble context melts back into the program.
Generally functions and variables have a complex interplay. Studying (typed or untyped) lambda calculus is all about this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With