Juliana Souza
Juliana Souza

Reputation: 69

Using mutual recursion

I want to execute this code, but I have this error. Is there mutual recursion in OCaml ?

Error: Unbound value quicksort Did you mean tquicksort?

My problem:

let rec tquicksort lm l lM = 
    match l with
        | [] -> unirL(unir (rev lm) lM)
        | lx::lxs -> let (la, lb) = part lx lxs in
                    if nroelem la < nroelem lb then 
                        tquicksort (quicksort (la::lm)) lb lM
                    else 
                        tquicksort lm la (quicksort (lb::lM));;
let quicksort l = tquicksort [] l [];;

Upvotes: 0

Views: 726

Answers (1)

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66823

You're asking for a forward declaration to allow mutual recursion. The way to make this work in OCaml is with let rec ... and ....

For example:

 let rec g x = if x < 2 then x else f x
 and f x = g (x - 1)

Upvotes: 5

Related Questions