epicours/Algo/Séminaire/Chapter 5 - Recursivity.md

2.9 KiB

5.1. Simple functions

To create a recursive function we have to use this structure

(*normal function*)
# let f = expr -> 1) evaluate expr ; 2) create & link f to the result

(*recursiv function*)
# let rec f = expr -> 1) create f ; 2) evaluate expr ; 3) link f to the result

Exmple with factorial function

# let rec fact = function 
	| 0 -> 1
	| n -> n * fact(n-1)

# fact 4;;
-: int = 24

Alt

⚠️ CAML have a call stack limite (it will block with stack overflow if the limit is reach)

Basic expression

let rec f params = 
	if stop condition then 
		stop expression
	else
		recursive expression 

To write a recurvise function we need two things

  1. There always is a stop case
  2. The parameters of a recursive call are different then its parent call, and go towards the stop condition

5.2. Down

# let rec countdown n = 
	if n < 0 then
		()
	else
		begin
			print_int n ;
			print_newline();
			countdown(n-1);
		end;;
val countdown = int -> unit = ()

# countdown 3;;
3
2
1
- : unit = ()

Flowchart of CAML evaluation

flowchart LR

A[ctd 3] --> B[ctd 2] --> C[ctd 1] --> D[ctd 0] --> E[ctd -1] --> F[ ] --> E --> D --> C --> B --> A

ctd is countdown

5.3. Several recursive calls

A function that have several recursive calls is defind like this


Fn = {1 -> \text{if } n\eqslantless{1} \brace{Fn+1+Fn+2} }

Fibonacci functions

let rec fibonacci n =
	if n = 0 then
		0
	else if n = 1 then
		1
	else
		fibonacci(n-1) + fibonacci(n-2);;

let rec odd n = 
	if n = 0 then
		false
	else
		even (n-1);;

and even n =
	if n = 0 then
		true
	else
		add (n-1);;
val add : int -> bool = <fun>
val even : int -> bool = <fun>

An accumulator is a thing that try to get our result. In CAML we trying to not use an accumulator in our program. In CAML the syntax for the accumulator (inv) is: (exemple with reverse_int exercise. See 4.9 - b)

# let reverse_int =
	let rec rev inv = function
		| 0 -> inv
		| n -> rev (inv*10 + n mod 10)(n/10)
	in rev 0 n;;

4.3. Complexity

Exemple with egypt (4.10) vs multiply (4.6):

# let multiply a b =
	let (a,b) = if a > b then
		(a,b) else
		(b,a)
	in let rec mult = function
		| 0 -> 0
		| b -> a +mult (b-1)
	in mult b;;

The best algorithm in term of complexity is the parameter that is constant/linear or logarithmic. If you have an exponential algorithm, you can put it in trash :)

Exemple with fibonacci algorithm

res how (for human) ? How (for function) ?
0 1 def 1 call
1 1 def 1 call
2 2 + 3 calls
3 3 + 5 calls
4 5 + 8 calls
5 8 + 11 calls
6 13 + 14 calls