-
-
Save marco-brandizi/f19a22f49adfcce1a4af56929fef121a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Il citato metodo della somma funziona con i numeri, ma anche con algebre che supportano operazioni analoghe a | |
somma e differenza. Es, con le stringhe (scusate, non ricordo bene il C): | |
a = a0; b = b0 // initial values | |
a += b // now it's a0 + b0 | |
b = substr ( a, 0, len ( b ) ) // i.e., b = a0 + b0 - b0 = a0 | |
a = substr ( min ( len ( b ), len ( a ) - 1 ) ) // i.e., a = a0 + b0 - b0 | |
Stessa roba si potrebbe fare con array e liste. | |
Nel caso più generale possibile, mi viene in mente questa versione funzionale/ricorsiva | |
(spero la sintassi sia giusta): | |
function <T> T swap_trick ( T &var, T x, T y ) { | |
var = y; | |
return x; | |
} | |
// Swap a and b | |
// The x in the external invocation can be whatever here, will just be passed back to the caller, which will | |
// ignore it | |
swap_trick ( a, NULL, swap_trick ( b, b, a ) ); | |
Formalmente non usa variabili di appoggio, però è un po' come barare, perché si appoggia al runtime stack. | |
Non credo si possa fare senza nessuna memoria di appoggio, sarebbe interessante dimostrarlo formalmente. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment