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
7
votes
3 answers
Why aren't there function headers in OCaml?
In certain programming languages, most notably in C, there are header files with function declarations. These function "headers" come before the code, and are required for situations with mutual recursion. When the function headers are placed in a…

fileyfood500
- 1,199
- 1
- 13
- 29
7
votes
4 answers
Mutually recursive classes
How do I implement mutually recursive classes in C++? Something like:
/*
* Recursion.h
*
*/
#ifndef RECURSION_H_
#define RECURSION_H_
class Class1
{
Class2* Class2_ptr;
public:
void Class1_method()
{
//...
…

user383352
- 5,033
- 4
- 25
- 20
7
votes
2 answers
Compiling Tail-Call Optimization In Mutual Recursion Across C and Haskell
I'm experimenting with the foreign-function interface in Haskell. I wanted to implement a simple test to see if I could do mutual recursion. So, I created the following Haskell code:
module MutualRecursion where
import Data.Int
foreign import ccall…

Crazycolorz5
- 747
- 6
- 12
6
votes
1 answer
How does one resolve F# Type Reference Errors?
I've been through my books, and I've googled until I've ran out of search terms, yet I still can't find an example or answer to this problem:
The following code does not compile because the type Effect and the type Affect have not been declared at…

Adam Lenda
- 700
- 6
- 13
6
votes
1 answer
F#: is mutual recursion between types and functions possible?
I can use the and keyword to set up mutually recursive function definitions. I can also use and for mutually recursive types, but what if there is a mutually recursive relationship between a type and a function? Is my only option to make the…

Keith
- 2,820
- 5
- 28
- 39
6
votes
2 answers
How do I remove this type of mutual recursion?
I am running into a mutual recursion issue. The basic structure I have employed is that I have a module that defines a type class and several modules that define instances of that type class. Each instance however is defined in terms of all the…

Wheat Wizard
- 3,982
- 14
- 34
6
votes
2 answers
Mutual recursion and JSLint - function was used before it was defined
If I write the following code, JSLint complains that 'isOdd' was used before it was defined. Is there a way to write mutually recursive code and still please JSLint?
var isEven = function(n) {
if (n === 0) {
return true;
}
return…

johnnyodonnell
- 1,838
- 3
- 16
- 34
6
votes
1 answer
How to define mutual inductive propositions in Lean?
I try using the syntax for inductive datatypes but it got an error message "mutually inductive types must compile to basic inductive type with dependent elimination".
Below is an example of my attempt to define propositions even and odd on natural…

Phil
- 5,595
- 5
- 35
- 55
6
votes
3 answers
Is hoisting really necessary in javascript to enable mutual recursion?
In an online course, Kyle Simpson says the following code demonstrates the necessity of hoisting in javascript, because without hoisting "one of the functions would always be declared too late."
a(1) // 39
function a(foo){
if (foo > 20) return…

rswerve
- 169
- 1
- 3
- 7
6
votes
2 answers
How to speed up (or memoize) a series of mutually recursive functions
I have a program which produces a series of functions f and g which looks like the following:
step (f,g) = (newF f g, newG f g)
newF f g x = r (f x) (g x)
newG f g x = s (f x) (g x)
foo = iterate step (f0,g0)
Where r and s are some uninteresting…
user328062
5
votes
1 answer
Can the F# compiler optimize these mutually recursive functions?
I wrote the following function that checks the validity of bracketed expressions:
let matched str =
let rec matched' stack = function
| "" -> isEmpty stack
| str ->
match first str with
| '(' | '[' | '{'…

Botond Balázs
- 2,512
- 1
- 24
- 34
5
votes
2 answers
Mutually recursive types in OCaml
In Haskell you can do the following:
Prelude> data Foo = Foo Bar; data Bar = Bar Foo
How can you do the same thing in OCaml? I tried:
___
# type foo = Foo of bar;; type bar = Bar of foo;;
Error: Unbound type constructor bar
Is…

Aadit M Shah
- 72,912
- 30
- 168
- 299
5
votes
3 answers
What is this kind of mutual "recursion" called?
My issue is with a certain style of code that very much resembles recursion, but isn't quite that. Recursion is, to quote Wikipedia, "a method of defining functions in which the function being defined is applied within its own definition". Similarly…

Devin Jeanpierre
- 92,913
- 4
- 55
- 79
5
votes
1 answer
How to get a working state machine in F# using functions for representing states?
I'm trying to create a simple state machine in F# but having trouble getting two states with circular dependencies to work.
I have this state factory:
open System
let createState produceInput stateSwitchRule nextState =
let rec stateFunc() =
…

Christian
- 7,433
- 4
- 36
- 61
5
votes
1 answer
How is this causing an endless loop?
Some legacy code I'm stuck maintaining is stuck in an infinite loop (and thus I myself seem to be in one); I can't figure out why/how, though.
Here's the app's entry point, where it instantiates the main form (frmCentral):
CODE EXHIBIT A
public…

B. Clay Shannon-B. Crow Raven
- 8,547
- 144
- 472
- 862