This is a general question about functional programming, but I'm also interested in what answer it has in certain languages.
I only have a basic knowledge of functional languages, so bear with me.
I understand that functional languages focus on different data structures than on imperative languages because they like immutability: persistent data structures.
For example, all of them have the concept of an immutable list, in which you can create new lists x :: l and y :: l from an existing list l and two new elements x and y without all elements l need to be copied. This is likely implemented by a new list object that internally points to the old one as a tail.
In imperative languages, such a data structure is rarely used because they do not provide good locality of links like c-style arrays.
In general, the search for data structures that support a functional style is his own ambition, so it would be great if it were not always so.
Now here is the idea of how you can use all the classic data structures in functional programming, if you have the right language support for this.
In general, a data structure in an imperative language has modifying operations defined on it (in pseudocode):
data.modify(someArgument)
Functional Recording Method:
newData = modified(data, someArgument)
The common problem is that this usually requires copying the data structure, except that the language can know that data will not actually be used by anyone else: then the modification can be made as a mutation of the original and no one could tell the difference.
There is a large class of cases where the language can output this property “never been used elsewhere”: when the first argument modified is an unrelated value, as in this example:
newData = modified(modified(data, someArgument))
Here, data can be used elsewhere, but modified(data, someArgument) clearly not.
This is what is called “rvalue” in C ++, and in the latest incarnation of C ++, which, ironically, doesn’t work at all, you can overload these rvalues.
For example, you can write:
Data modified(Data const& data) {
This means that in C ++ you can actually use any mutable efficient data structure and convert it to an immutable api, which can be used in a purely functional way as efficiently as the imperative version.
(There is a warning that casting is still sometimes performed in C ++, you must force the rvalue to be overloaded. And, of course, you must take care of the implementation of such data structures, that is, when using rvalue overloads ..)
Now my question is:
Do valid functional languages have a similar mechanism? Or is it not needed for some other reason?
(I noted some specific languages that interest me especially.)