Questions tagged [mutual-recursion]

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.

104 questions
5
votes
4 answers

Example demonstrating good use of mutual recursion

I would like to know if there is a non-artificial example, where mutual recursion is the most elegant solution to a problem and it cannot be easily reduced/inlined to a single recursive function. I already know this example (from Wikipedia) function…
user267885
4
votes
1 answer

How to define an inductive type mutually recursive with a function?

I want to define an inductive type Foo, with constructors accepting as arguments some properties. I want those properties to depend on inductive arguments of the type I am currently defining. I want to be able to collect some data from them inside…
Joald
  • 1,114
  • 10
  • 32
4
votes
3 answers

Unable to understand a mutual recursion

I am reading Programming In Haskell, in the 8th chapter, the author gives an example of writing parsers. The full source is here: http://www.cs.nott.ac.uk/~gmh/Parsing.lhs I can't understand the following part: many permits zero or more…
Sawyer
  • 15,581
  • 27
  • 88
  • 124
4
votes
1 answer

How to initialize mutually recursive records in F#

I have two records that have a parent-child relationship: type Parent = { Number: int Child: Child } and Child = { String: string Parent: Parent } I have tried initializing these using the following syntax, which doesn't work: let rec…
cmeeren
  • 3,890
  • 2
  • 20
  • 50
4
votes
2 answers

Coq best practice: mutual recursion, only one function is structurally decreasing

Consider the following toy representation for the untyped lambda calculus: Require Import String. Open Scope string_scope. Inductive term : Set := | Var : string -> term | Abs : string -> term -> term | App : term -> term -> term. Fixpoint print…
Carl Patenaude Poulin
  • 6,238
  • 5
  • 24
  • 46
4
votes
1 answer

F#: Catamorphisms for mutually recursive data structures

Assume the following mutually recursive structure: type Tree<'a> = | Empty | Node of 'a * 'a Forest and Forest<'a> = | Nil | Cons of 'a Tree * 'a Forest Goal: Generate the common catamorphisms for this structure: foldl, foldr,…
4
votes
4 answers

Is it possible to define types that depend on each other and are defined in separated files?

I am trying to implement a library with extended parsing capabilities. I decided that I will use fsyacc because I knew it from the university. Unfortunately I encountered following problem. I defined a class for the head of my grammar (Head) and…
StanislawSwierc
  • 2,571
  • 17
  • 23
4
votes
2 answers

How can I reorder these F# functions to make sense?

I thought I'd be getting along alright with F# since I'm decent at Haskell, but I feel like I'm being stumped by dead simple issues. I have some parsing code for a simple JSON parser, like this: let rec parseObject tokens = function | '"' :: cs ->…
CodexArcanum
  • 3,944
  • 3
  • 35
  • 40
4
votes
1 answer

C++ Mutually Recursive Variant Type

I am trying to represent a PDF object type in c++ using variants. A PDF object is one of the following: Boolean Integer Real String Name Stream Array Map As you can see, the Object type is mutually recursive because the…
Ell
  • 4,238
  • 6
  • 34
  • 60
4
votes
1 answer

How do the implementation of Eq typeclass function: x == y = not (x /= y) x /= y = not (x == y) work?

I am reading the book, and it talks about the definition of typeclass Eq There are two functions ==, /= in the Eq, and they are implemented as: x == y = not (x /= y) x /= y = not (x == y) The book says that they are mutual recursion, the…
code4j
  • 4,208
  • 5
  • 34
  • 51
3
votes
3 answers

defining mutually dependent variables

I need to define mutually-dependent vars. By this I mean that one var contains e.g. a vector with another var and vice versa. This is illustrated by the following code: (declare a b) (def a [1 b]) (def b [a 2]) But after loading this code I get…
Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296
3
votes
2 answers

Julia: Question about variable binding, mutating, and mutable functions

I am writing code in Julia which collects some output from a function foo (which mutates its input argument), and I'm trying to append the recursive evaluations from this function in an array A. For instance, foo!(x) changes the value of x by adding…
SidV
  • 45
  • 4
3
votes
2 answers

How to translate the poly-variadic fix-point combinator to a strict language?

I am attempting to translate the following Haskell code to Javascript: fix_poly :: [[a] -> a] -> [a] fix_poly fl = fix (\self -> map ($ self) fl) where fix f = f (fix f) I have trouble understanding ($ self) though. Here is what I've achieved so…
user5536315
3
votes
1 answer

Stack-safe mutual recursion without leaking implementation details on the call side

I generalized clojure's loop/recur trampoline so that it works with indirect recursion: const trampoline = f => (...args) => { let acc = f(...args); while (acc && acc.type === recur) { let [f, ...args_] = acc.args; acc =…
user5536315
3
votes
1 answer

how to write mutually recursive functions in Haxe

I am trying to write a simple mutually recursive function in Haxe 3, but couldn't get the code to compile because whichever one of the mutual functions that appears first will report that the other functions in the group is undefined. A minimal…
thor
  • 21,418
  • 31
  • 87
  • 173