Liste des Groupes | Revenir à cl c |
On Thu, 11 Jul 2024 08:41:14 -0000 (UTC)
Kaz Kylheku <643-408-1753@kylheku.com> wrote:
On 2024-07-11, Michael S <already5chosen@yahoo.com> wrote:
There are multiple classes of functions. Closures are part of it but there are several versions of closures too:Thank you.I fail to see a material difference between first class function>
values in Haskell and C++ and first class function pointer values
in C:
>
int doubler(int x) {
return x*2;
}
int doTwice(int (*foo)(int), int x) {
return foo(foo(x));
}
int quadrupler(int x) {
return doTwice(doubler, x);
}
>
I am willing to believe that the difference exists, but your
example is too basic to demonstrate it.
First class functions could do something like this:
>
// multiplier takes a coefficient and returns a pointer to
// function
>
int (*multiplier(int coefficient))(int) {
// fantasy lambda syntax. Return type int is written after
// parameter list.
return lambda(int x) int {
return coefficient * x;
}
}
>
int (*doubler)(int) = multiplier(2);
>
int x = doubler(42); // x becomes 84
>
Even though the lambda is returned out of multiplier, whose execution
terminates, when the returned function is invoked, it can refer to the
coefficient, which is captured in a lexical closure.
>
With a C-like typedef, we can declutter the definition of mutiplier:
>
typedef int (*int_int_fn)(int);
>
int_int_fn multiplier(int coefficient) {
return lambda(int x) int {
return coefficient * x;
}
}
>
Your example confirms my suspicion that the difference between first
and second class of functions doesn't become material until language
supports closures.
Les messages affichés proviennent d'usenet.