Rekursion

Rekursion

Kernkonzept

Die Funktion ruft sich selbst auf

Important

  • Es muss unbedingt eine Terminierung stattfinden, weil man sonst ein Endlosprogramm erstellt
  • Nie Abbruchbedingung vergessen
  • Man braucht bei einer Rekursion zwangsweise einen "Fortschritt", sonst Endlosprogramm trotz Terminierung

Pythagoras Baum

Pythagoras Baum.png

Romanesco

Romanesco.png

Schleife vs. Rekursion

float potenz(float x, int n) { 
	float sum = 1; 
	while (n--) sum *= x; 
	return sum; 
} 
potenz :: Double -> Int -> Double 
potenz x n = if n == 0 then 1 -- Rekursionsanker 
	else x * (potenz x (n - 1))
	```