Capitulo 6
Control Flow o Ordering
Determina que se hace primero y que después en determinadas tareas.
int foo(int x) {
return x*2;
}
int sum(int n){
if (n<=0) then
return 0
else
return n+sum(n-1)
}
int main() {
int a = 2
a = foo(a)
foo(1);
a = sum(10)
int b = 2
int x = a/b
return 0
}
loop
toss coin
if heads, send read to server
if tails, send write to server
loop
receive Read:
send data
OR
receive Write:
send reply
Cliente
Servidor
Communicating Sequential Processes
if (a>b) max = a;
else max = b;
if (a>=b) max = a;
else max = b;
//Esto siempre produce el mismo resultado si a=b
if a>=b -> max:=a;
[] b>=a -> max:=b;
fi
//El resultado de esto es aleatorio si a=b
x ** y = x ^ y
// entonces
2**3**4
//equivale a 2^(3^4) en vez de (2^3)^4.
caso de contextos
Imperativo
int sum(int n){
int val=0;
for(int i=0,i<=n;i++){
val+=i;
}
return val;
}
Funcional
int sum(int n){
if (n<=0) then
return 0
else
return n+sum(n-1)
}
Imperativo
//comparativo javascript
function sum1(n) {
if(n<=0)
return 0;
else
return n + sum1(n - 1);
}
function sum2(n) {
var val = 0;
for(var i=0;i<=n;i++)
val+=i;
return val
}
x = sum2(10);
b = 2;
a = b;
seria
mem(1024)= 2;
mem(2036)= 2;
b = 2;
a = b;
mem(1024)= loc(2);
mem(2036)= mem(1024);
// Implicita
$a = 3+8;
//Explicita
int z;
z=1+1;
Orthogonal = statistically independent
Significa que una caracteristica del L.P se puede utilizar en cualquier combinacion y el significado se mantiene consistente. Algol 68
Cambiar X no cambia B
Orthogonality System
Radio: Cambiar de estacion no cambia el Volumne.
Non-Orthogonality System
Helicoptero: Cambiar la velicidad, cambia la direccion.
if(a=b){ }
if(a==b){ }
//Non-Orthogonality C
//If you have a set of constructs.
//A language is said to be orthogonal if it allows the programmer
//to mix these constructs freely.
//For example, in C you can't return an array(static array),
//C is said to be unorthognal in this case:
int[] fun(); // you can't return a static array.
// Of course you can return a pointer, but the P.L. allows passing arrays.
// So, it is unorthognal in case.
p = my_list;
while (p && p->key != val)
p=p->next
GOTO statement
//Enumeracion
for (int i=0;i<=10;i++){
...
}
//Logico
int i = 0;
while (i<=10){
...
i++;
}
//Variantes: Post-test, Midtest y Pre-test
int gcd(int a,int b){
if(a ==b) return a;
else if (a>b) return gcd(a-b,b);
else return gcd(a,b);
}
int gcd(int a,int b){
while(a!=b){
if(a>b) { a = a-b;}
else {b = b-a;}
}
return a;
}
int gcd(int a,int b){
if(a ==b) return a;
else if (a>b) return gcd(a-b,b);
else return gcd(a,b);
}
int gcd(int a,int b){
int x;
if(a ==b) x=a;
else if (a>b) x= gcd(a-b,b);
else x= gcd(a,b);
return x;
}
Tail Recursion
La ventaja de la Tail Recursion, es que el espacio se puede re-utilizar