4 ms·
Well, that depends a bit what you mean by "function-defining expression". Let's say `(new function(input) { return input })` is a valid expression. If it then
by valenterry 4y ago
Well, that depends a bit what you mean by "function-defining expression".
Let's say `(new function(input) { return input })` is a valid expression. If it then doesn't matter (in any circumstance) whether you do
let myFunction = (new function(input) { return input })
myFunction(1)
myFunction(2)
or
(new function(input) { return input })(1)
(new function(input) { return input })(2)
that's when you can call the function-defining expression to be referentially transparent. If, on the contrary, creating a function increases a counter and you use that counter in your programing for something and it makes your program behave differently depending on how many functions are created, then the function creating expression would cease to be referential transparent.
I think that's not a bad example actually, because I assume it feels more natural to people to be able to treat an expression that creates a function in the way above and expecting that it won't change how the program behaves. And this is really what functional programming is about - it is exactly this feeling and guarantee that FP enforces throughout the whole application, not just in some places.
If you are talking about creating a named function (i.e. a class member) that is not an expression, then there isn't really much point in talking about it since we are now talking about definitions, not expressions anymore. Unless you can change/create definitions programmatically - in that case the code that does it programmatically is the one that needs to be considered, not the definition itself.
- kazinator 4y agoWhat is the value of let myFunction = ... which can replace that whole construct so that the program remains the same? Or is this not functional?
- valenterry 4y agoI think you are confused. FP doesn't require any kind of token/syntax to be "replacable". It requires expressions to be "replacable" (RT). A language could certainly treat assignments as expressions (some do) and have them return something, such as the value that is assigned to the variable or always null/unit. But we are now talking about a totally different thing.
- valenterry 4y agoTo give a concrete example: Haskell supports something called do-notation. You cannot "replace" parts of it, because it is syntactic sugar that will turn into a number of expressions. But all of those expressions are then RT. In the same way you can have a C++ program that is FP (completely RT) but uses templates, which are obviously not "replacable". I hope that makes it more clear.
- kazinator 4y agoI'm just trying to follow the proposed definition of FP. Turns out there are non-expressions present; yet somehow if just the expressions are referentially transparent, we have FP? We have to say something about the kinds of non-expression thingys that are required/allowed and what their properties must be. Assignments can be expressions, but they are not referentially transparent. If an assignment is replaced by its value, it's no longer performing the assignment. Hence, assignments are easily found to be incompatible with FP under the RT definition, which underscores that it is useful.
- valenterry 4y ago> Turns out there are non-expressions present; yet somehow if just the expressions are referentially transparent, we have FP? Why "just"? > We have to say something about the kinds of non-expression thingys that are required/allowed and what their properties must be. Well, if there are syntactical constructs that will execute code at runtime, then we can essentially consider them expressions, even though sometimes they are restricted in how they can be used. For example, in Javascript and Java there is an if-syntax (call it if-statement or whatever) that will execute code but it doesn't return anything and you can "assign" the result to a variable. Obviously this breaks referential transparency (or can't do anything meaningful). We can still see it as an expression for the purpose of the definiton. Other than that, as I said in my parallel post, if you have something like templates (which are not executing code at runtime), then they can do whatever you want, the only thing that matters is what expressions they will produce and if those are RT or not to fullfill the definition of FP. > If an assignment is replaced by its value, it's no longer performing the assignment And the funny thing is, it doesn't matter. Your code might stop compiling because you refer to a variable that isn't declared, yeah. And that's about it. But otherwise in a fully RT program it won't change the bebaviour of the program. EXCEPT if it is a reassignment. Which is why reassignments are violating RT and are therefore not a thing in FP - there can be specific exceptions (i.e. shadowing something in a different scope) but those aren't really reassignments, they just sometimes use the same syntax.