Mutual recursion is a case in computer science where multiple problems that depend on each other form a cycle, like the chicken and egg problem.
Questions tagged [mutual-recursion]
104 questions
39
votes
4 answers
How to have two methods calling each other?
I'm a bit confused as to how to get two method to call each other (i.e., have A() call B() and B() call A()). It seems that F# only 'sees' the method after it's been encountered in code, so if it hasn't, it just says value or constructor has not…

Dmitri Nesteruk
- 23,067
- 22
- 97
- 166
35
votes
3 answers
F# forward type declarations
I stumbled across this problem in F#. Suppose, I want to declare two types that reference each other:
type firstType =
| T1 of secondType
//................
type secondType =
| T1 of firstType
//................
How do…

Max
- 19,654
- 13
- 84
- 122
18
votes
1 answer
Why doesn't Haskell support mutually recursive modules?
Haskell supports mutually recursive let-bindings, which is great. Haskell doesn't support mutually recursive modules, which is sometimes terrible. I know that GHC has its .hs-boot mechanism, but I think that's a bit of a hack.
As far as I know,…
user824425
17
votes
3 answers
Fixed point combinator for mutually recursive functions?
Is there a fixed point combinator for creating tuples of mutually recursive functions? I.e. I'm looking for something like the Y-Combinator but which takes multiple "recursive"* functions, and will return a tuple of functions?
*: not really…

pauldoo
- 18,087
- 20
- 94
- 116
17
votes
1 answer
Haskell let expression converges while similar expression using fix does not
I have been having difficulty understanding why the haskell expression let (x,y) = (y,1) in (x,y) converges to (1,1) as expected but fix (\(x,y)-> (y,1)) results in <> being thrown. Can anyone explain this?

Alexander Wittmond
- 213
- 1
- 5
14
votes
3 answers
Can discriminated unions refer to each other?
I'm building an expression tree using discriminated unions. The below code:
type IntExpression =
| TrueIsOne of BoolExpression
type BoolExpression =
| LessThan of IntExpression * IntExpression
| And of BoolExpression * BoolExpression
…

mavnn
- 9,101
- 4
- 34
- 52
13
votes
4 answers
Problem determining how to order F# types due to circular references
I have some types that extend a common type, and these are my models.
I then have DAO types for each model type for CRUD operations.
I now have a need for a function that will allow me to find an id given any model type, so I created a new type for…

James Black
- 41,583
- 10
- 86
- 166
12
votes
3 answers
F#: Mutually recursive functions
Possible Duplicate:
[F#] How to have two methods calling each other?
Hello all,
I Have a scenario where I have two functions that would benefit from being mutually recursive but I'm not really sure how to do this in F#
My scenario is not as…

rysama
- 1,674
- 16
- 28
12
votes
2 answers
How to have two functions that call each other C++
I have 2 functions like this that does obfuscation on if loop:
void funcA(string str)
{
size_t f = str.find("if");
if(f!=string::npos)
{
funcB(str); //obfuscate if-loop
}
}
void funcB(string str)
{
//obfuscate if…

consprice
- 722
- 3
- 11
- 21
11
votes
2 answers
OCaml: Declaring a function before defining it
Is there a way to declare a function before defining it in OCaml? I'm using an OCaml interpreter.
I have two functions:
let myFunctionA =
(* some stuff here..... *) myFunctionB (*some stuff *)
let myFunctionB =
(* some stuff here .... *)…

Casey Patton
- 4,021
- 9
- 41
- 54
10
votes
1 answer
Organise my mutual recursive types
Is it possible to have mutual recursive types ([]) 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?

elmattic
- 12,046
- 5
- 43
- 79
9
votes
2 answers
Why the requirement for signatures in mutually recursive modules in OCaml?
When using mutually recursive module definitions in OCaml, it's necessary to give signatures, even in the .ml file. This is an annoyance where I also want to expose a given interface from the .mli, as I end up repeating the signature twice.…

Asherah
- 18,948
- 5
- 53
- 72
9
votes
4 answers
How does python implement lookups for mutually recursive function calls?
Suppose I have two functions one after another in the same python file:
def A(n):
B(n-1)
# if I add A(1) here, it gives me an error
def B(n):
if n <= 0:
return
else:
A(n-1)
When the interpreter is reading A, B is not yet…

watashiSHUN
- 9,684
- 4
- 36
- 44
8
votes
3 answers
What is the standard way to optimise mutual recursion in F#/Scala?
These languages do not support mutually recursive functions optimization 'natively', so I guess it must be trampoline or.. heh.. rewriting as a loop) Do I miss something?
UPDATE: It seems that I did lie about FSharp, but I just didn't see an example…

Bubba88
- 1,910
- 20
- 44
7
votes
4 answers
Getting even and odd position of elements in list - Haskell Mutual Recursion
I recently started learning Haskell.
I found this code online which returns the elements at all even/odd positions of a list.
It makes use of mutual recursion, but I cannot seem to understand how it works internally.
evens (x:xs) = x:odds xs
evens…

ArshSoni
- 147
- 3
- 7