Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organise my mutual recursive types

Is it possible to have mutual recursive types ([<Struct>]) spread across different files? The types are directly under a namespace.

My solution is to put them in one big file and use type ... and ... and ... etc construction. Is it the only way?

like image 722
Stringer Avatar asked Nov 29 '10 21:11

Stringer


People also ask

Where are we most likely to find mutual recursion?

Mutual recursion is very common in functional programming and in some problem domains, such as recursive descent parsers, where the datatypes are naturally mutually recursive.

What is mutual recursion in C?

Mutual recursion is a variation recursion. Two functions are called mutually recursive if the first function makes a recursive call to the second function and the second function, in turn, calls the first one.


1 Answers

You can use a technique called untying the recursive knot where you parameterize one over the other.

So this:

type a = B of b
and b = A of a

becomes:

type 'b a = B of 'b
type b = A of b a
like image 169
J D Avatar answered Sep 17 '22 15:09

J D