Esta es la clase que dí en el concurso para el cargo de ayudante de trabajos prácticos para la materia Sintaxis y Semántica del Lenguaje de la Universidad Tecnológica Nacional, Facultad Regional La Plata, en el año 2014.
el CTE 6 DOCENTES 2 2023-2024abcdefghijoklmnñopqrstuvwxyz
Clase concurso sintaxis 2014
1. UTN FRLP
Ing. en Sistemas de información
Concurso docente
Sintaxis y Semántica del Lenguaje
2014
Ing. Julián Perelli
2. TEMARIO
Técnicas de recursión
Introduccion – Ejemplos de uso
Definición
Ejemplos de programación
Backtracking
Recursión vs iteración
Utilidad de la recursión
UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 2/68
3. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 3/68
Problemas de naturaleza recursiva
Parsing matemático: calculadora con paréntesis
2 × (a −1) + (3 × b)
9. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 9/68
¿Qué es la recursión?
10. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 10/68
Frase popular
Para saber qué es la recursión
primero hay que saber
¿QUÉ ES LA RECURSIÓN?
11. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 11/68
Frase popular
Para saber qué es la recursión
primero hay que saber
qué es la recursión.
¿QUÉ ES LA RECURSIÓN?
12. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 12/68
Algoritmo recursivo:
un algoritmo que depende de la ejecución
repetida de sí mismo.
¿QUÉ ES LA RECURSIÓN, REALMENTE?
13. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 13/68
Algoritmo recursivo:
un algoritmo que depende de la ejecución
repetida de sí mismo.
¿QUÉ ES LA RECURSIÓN, REALMENTE?
?
14. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 14/68
Algoritmo recursivo:
Function recursionInfinita();
Begin
recursionInfinita()
end
¿QUÉ ES LA RECURSIÓN, REALMENTE?
15. ∞
UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 15/68
Algoritmo recursivo:
un algoritmo que depende de la ejecución repetida
de sí mismo.
Function recursionInfinita()
Begin
recursionInfinita();
end
recursionInfinita()
¿QUÉ ES LA RECURSIÓN, REALMENTE?
Function recursionInfinita()
Begin
recursionInfinita();
end
Function recursionInfinita()
Begin
recursionInfinita();
end
Function recursionInfinita()
Begin
recursionInfinita();
end
16. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 16/68
Hacer un procedimiento recursivo que muestre
por pantalla un número dado “n”.
EJERCICIO 1
17. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 17/68
Hacer un procedimiento recursivo que muestre
por pantalla un número dado “n”.
Procedure contar(n: Integer);
Begin
WriteLn(n);
contar(n);
End
EJERCICIO 1
18. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 18/68
Hacer un procedimiento recursivo que muestre
por pantalla un número dado “n”.
Procedure contar(n: Integer);
Begin
WriteLn(n);
contar(n);
End
EJERCICIO 1
Llamada recursiva
19. contar(5)
UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 19/68
Hacer un procedimiento recursivo que muestre
por pantalla un número dado “n”.
Procedure contar(n: Integer);
Begin
WriteLn(n);
contar(n);
End
EJERCICIO 1
Llamada recursiva
UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 19/68
Function contar(n = 5)
Begin
WriteLn(n = 5);
contar(n = 5)
end
contar(5)Function contar(n = 5)
Begin
WriteLn(n = 5);
contar(n = 5)
end
contar(5)Function contar(n = 5)
Begin
WriteLn(n = 5);
contar(n = 5)
end
contar(5)
20. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 20/68
Cada llamada ocupa memoria que nunca se libera,
porque ningún procedimiento termina.
Computación limitada por tamaño de memoria.
Se genera un desbordamiento de pila.
¡ESTO NO SIRVE!
21. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 21/68
1. Llamada recursiva (por definición).
2. Condición de corte.
3. Cada llamada recursiva converge (se acerca)
paso a paso a la condición de corte.
ALGORITMO RECURSIVO ÚTIL
22. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 22/68
Hacer un procedimiento recursivo que muestre por
pantalla todos los números naturales que existen desde
un número dado “n” hasta 100.
EJERCICIO 2
23. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 23/68
Hacer un procedimiento recursivo que muestre por pantalla
todos los números naturales que existen desde un número dado
“n” hasta 100.
procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
EJERCICIO 2
24. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 24/68
EJERCICIO 2
1. Llamada recursiva
3. Acercamiento a la condición de corte
2. Condición de corte
Hacer un procedimiento recursivo que muestre por pantalla
todos los números naturales que existen entre un número dado
“n” hasta 100.
procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
25. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 25/68
EJERCICIO 2
procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
contar(98)
Impresión
(registro de activación)
26. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 26/68
EJERCICIO 2
procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
contar(98)
98
Impresión
27. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 27/68
EJERCICIO 2
contar(98)
procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
98
Impresión
98
28. procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 28/68
EJERCICIO 2
contar(98)
98 < 100 (true)
Impresión
98
29. procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 29/68
EJERCICIO 2
contar(98)
contar(98 +1)
Impresión
98
30. 99
procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 30/68
EJERCICIO 2
contar(98)
contar(99)
procedure contar(n: intege
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
98
Impresión
98
31. procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 31/68
EJERCICIO 2
contar(98)
contar(99)
procedure contar(n: intege
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
98
99
Impresión
98
99
32. procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 32/68
EJERCICIO 2
contar(98)
contar(99)
procedure contar(n: intege
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
98
99 < 100 (true)
Impresión
98
99
33. procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 33/68
EJERCICIO 2
contar(98)
contar(99)
procedure contar(n: intege
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
98
contar(99 +1)
Impresión
98
99
34. procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 34/68
EJERCICIO 2
contar(98)
contar(99)
procedure contar(n: intege
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
98
contar(100)
procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
100
99
Impresión
98
99
100
35. procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 35/68
EJERCICIO 2
contar(98)
contar(99)
procedure contar(n: intege
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
98
contar(100)
procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
99
100 < 100 (false)
Impresión
98
99
100
36. procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 36/68
EJERCICIO 2
contar(98)
contar(99)
procedure contar(n: intege
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
98
contar(100)
procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
99
100 < 100 (false)
Impresión
98
99
100
37. procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 37/68
EJERCICIO 2
contar(98)
contar(99)
procedure contar(n: intege
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
98 99
Impresión
98
99
100
38. procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 38/68
EJERCICIO 2
contar(98)
contar(99)
procedure contar(n: intege
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
98 99
Impresión
98
99
100
39. procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 39/68
EJERCICIO 2
contar(98)
98
Impresión
98
99
100
40. procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 40/68
EJERCICIO 2
contar(98)
98
Impresión
98
99
100
42. Hacer un procedimiento recursivo que muestre por pantalla
todos los números naturales que existen entre un número dado
“n” hasta 100 en orden inverso.
procedure contar(n: integer);
begin
WriteLn(n);
if n < 100 then
begin
contar(n+1);
end
end
UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 42/68
EJERCICIO 2b
2° recursión
1° impresión, proceso, acción
43. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 43/68
EJERCICIO 2b
1° recursión
2° impresión, proceso, acción
Hacer un procedimiento recursivo que muestre por pantalla
todos los números naturales que existen entre un número dado
“n” hasta 100 en orden inverso.
procedure contar(n: integer);
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
44. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 44/68
EJERCICIO 2b
1° recursión
2° impresión, proceso, acción
Hacer un procedimiento recursivo que muestre por pantalla
todos los números naturales que existen entre un número dado
“n” hasta 100 en orden inverso.
procedure contar(n: integer);
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
Backtracking
45. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 45/68
EJERCICIO 2b
procedure contar(n: integer);
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
contar(98)
Impresión
(registro de activación)
46. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 46/68
EJERCICIO 2b
procedure contar(n: integer);
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
contar(98)
Impresión
98
47. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 47/68
EJERCICIO 2b
procedure contar(n: integer);
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
contar(98)
Impresión
98 < 100 (true)
98
48. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 48/68
EJERCICIO 2b
contar(98)
Impresión
procedure contar(n: integer);
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
contar(98 +1)
98
49. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 49/68
EJERCICIO 2b
contar(98)
Impresión
procedure contar(n: integer);
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
contar(99)
procedure contar(n: integer
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
98 99
50. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 50/68
EJERCICIO 2b
contar(98)
Impresión
procedure contar(n: integer);
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
contar(99)
procedure contar(n: integer
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
98 99
99 < 100 (true)
51. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 51/68
EJERCICIO 2b
contar(98)
Impresión
procedure contar(n: integer);
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
contar(99)
procedure contar(n: integer
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
98 99
contar(99+1)
52. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 52/68
EJERCICIO 2b
contar(98)
Impresión
procedure contar(n: integer);
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
contar(99)
procedure contar(n: integer
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
98 99
procedure contar(n: integer);
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
contar(100)
100
100 < 100 (false)
53. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 53/68
EJERCICIO 2b
contar(98)
Impresión
100
procedure contar(n: integer);
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
contar(99)
procedure contar(n: integer
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
98 99
procedure contar(n: integer);
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
contar(100)
100
100
54. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 54/68
EJERCICIO 2b
contar(98)
Impresión
100
procedure contar(n: integer);
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
contar(99)
procedure contar(n: integer
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
98 99
procedure contar(n: integer);
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
contar(100)
100
55. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 55/68
EJERCICIO 2b
contar(98)
Impresión
100
procedure contar(n: integer);
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
contar(99)
procedure contar(n: integer
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
98 99
56. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 56/68
EJERCICIO 2b
contar(98)
Impresión
100
99
procedure contar(n: integer);
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
contar(99)
procedure contar(n: integer
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
98 99
99
57. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 57/68
EJERCICIO 2b
contar(98)
Impresión
100
99
procedure contar(n: integer);
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
contar(99)
procedure contar(n: integer
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
98 99
58. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 58/68
EJERCICIO 2b
contar(98)
Impresión
100
99
procedure contar(n: integer);
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
98
59. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 59/68
EJERCICIO 2b
contar(98)
Impresión
100
99
98
procedure contar(n: integer);
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
98
98
60. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 60/68
EJERCICIO 2b
contar(98)
Impresión
100
99
98
procedure contar(n: integer);
begin
if n < 100 then
begin
contar(n+1);
end
WriteLn(n);
end
98
63. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 63/68
Hacer un procedimiento iterativo equivalente al
ejercicio 2.
EJERCICIO 3
64. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 64/68
Hacer un procedimiento iterativo equivalente al
ejercicio 2.
EJERCICIO 3
Recursivo
procedure contar(n:
integer);
begin
WriteLn(n);
if n < 100 then
contar(n+1);
end
Iterativo
procedure contar(n:
integer);
begin
do
WriteLn(n);
n := n + 1;
until n >= 100
end
65. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 65/68
Hacer un procedimiento iterativo equivalente al
ejercicio 2.
EJERCICIO 3
Iterativo
procedure contar(n:
integer);
begin
do
WriteLn(n);
n := n + 1;
until n >= 100
end1. Llamada recursiva / bucle
3. Acercamiento a la condición de corte
2. Condición de corte
Recursivo
procedure contar(n:
integer);
begin
WriteLn(n);
if n < 100 then
contar(n+1);
end
66. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 66/68
OBSERVACIONES
Recursión lineal
- Una sola llamada recursiva.
- Puede ser reemplazada por un bucle while fácilmente.
- Sirve para recorrer estructuras lineales como arrays o
listas.
67. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 67/68
OBSERVACIONES: Utilidad de la recursión
Recursión binaria
- 2 llamadas recursivas dentro de cada bloque.
No se puede reemplazar por
un bucle while fácilmente
Sirve para recorrer estructuras de
árboles y grafos
Problemas de naturaleza
recursiva
68. UTN – FRLP – Concurso Sistemas Operativos 2014 - Ing. Julián Perelli 68/68
Recapitulación
Recursión:
Usos comunes
Definición
Definición + utilidad
Backtracking
Recursión vs while
Observaciones finales: utilidad