Which function results from primitive recursion of the functions g and h?


Which function results from primitive recursion of the functions $ g$ and $ h$ ?

  1. $ f_1=PR(g,h)$ with $ g=succ\circ zero_0, h=zero_2$
  2. $ f_2=PR(g,h)$ with $ g=zero_0, h=f_1\circ P_1^{(2)}$
  3. $ f_3=PR(g,h)$ with $ g=P_1^{(2)}, h=P_2^{(4)}$
  4. $ f_4=PR(g,h)$ with $ g=f_3\left(f_1(x),succ(x),f_2(x)\right)$

(1.) $ g:N^0\to N$ , $ h:N^2\to N$
$ f(0)=1$
$ f(0+1)=h(0,f(0))=h(0,1)=0$
$ f(1+1)=h(1,f(1))=h(1,0)=0$
$ \forall n\in N_{>0}:f(n+1)=h(n,f(n))=0$ , $ f_1$ is defined as $ f_1:N^1\to N$ with $ f_1(x)=\begin{cases}1, x=0\ 0, x>0\end{cases}$

(2.) $ g:N^0\to N$ , $ h:N^2\to N$
$ f(0)=0$
$ f(0+1)=h(0,f(0))=h(0,0)=1$ $ f(1+1)=h(1,f(1))=h(1,1)=0$ $ \forall n\in N_{>0}: f(n+1)=h(n,f(n))=0$ , $ f_2$ is defined the same as $ f_1$ , $ f_1(x)=f_2(x)$

(3.) $ g:N^2\to N$ , $ h:N^4\to N$
$ f(x,y,0)=x$
$ f(x,y,0+1)=h(x,y,0,f(x,y,0))=h(x,y,0,x)=y$ $ f(x,y,1+1)=h(x,y,1,f(x,y,1))=h(x,y,1,y)=y$ $ \forall z \in N_{>0}: f(x,y,z+1)=h(x,y,z,f(x,y,z))=y$ , $ f_3$ is defined as $ f_3:N^3\to N$ with $ f_3(x,y,z)=\begin{cases}x, z=0\ y, z>0\end{cases}$

Is this correct up to here? It looks way too easy, that’s why I’m not sure.

Why does Mathematica crash at a certain recursion depth?

If I enter

Block[{$  RecursionLimit = 70000}, x = x + 1] 

I get

$  RecursionLimit: Recursion depth of 70000 exceeded during evaluation of 1+x. 

But at $ RecursionLimit = 80000, Mathematica crashes (i.e. goes unresponsive for a little while and then clears all variables). Why is this? Is there some limiting factor that I can increase to go even further?

Any reason why Turing Machine would prevail on recursion theory?

Nowadays, most introduction books, videos, and comments about theoretical computer science talk about Turing machines but don’t discuss recursion theory anymore. These approaches are known to be equivalent.

What is the reason behind this? Has recursion theory simply gone out of fashion or is there a fundamental/mathematical reason explaining this?

What is sideways recursion

A friend of mine is studying business analytics, currently on the topic for Microsoft DAX, but he is very new to the technological field. He mentioned yesterday, during a conversation, the term "sideways recursion". Does anyone have idea what that means? I know a regular recursion(maybe direct recursion is the more proper term), where a function calls itself directly. I know of indirect recursion, where a function calls another and then the other calls the first one back. I also know that direct recursion has some sub-types – head / tail recursion – the tail recursion is when the inner call does not participate in any calculations including the current context (e.g. the recursive call is the last statement in the function) – this is very useful lately in functional languages to optimize recursive calls, by not keeping the current context in the stack, since it will not be needed when returning back from the recursive call. Those optimizations make functional languages as effective as imperative programming languages and allow the immutability of functional languages to become practical for traditional tasks, but also allows to implement parallelism automatically to some extent … as far as I know this is why functional programming languages started to become modern lately. I have also heard of variadic template recursive functions in C++, which allow to write recursive function with arbitrary number of arguments (not sure if arbitrary type is allowed as well). But I cannot find anything about "sideways recursion" … is there a chance Microsoft is using this term instead of one of the above mentioned? E.g. instead of "tail recursion" … or maybe instead of "variadic template recursive function" (even though this one is not exactly separate type of recursion, but more a technology paradigm from the language C++)? Any help on making my knowledge on recursion classification and technology paradigms around different types of recursion would be appreciated! Thanks in advance! 🙂

Arbitrary depth nested for-loops without recursion

Suppose I have an array of n values I want to apply nested for-loops over to an arbitrary depth m.

const array = [1, 2, 3];  // 2-depth for-loop for (const i of array) {   for (const j of array) {     // do the thing   } }  // 3-depth for-loop for (const i of array) {   for (const j of array) {     for (const k of array) {       // do the thing     }   } } 

The obvious solution is to use recursion. In JavaScript/TypeScript, a generator lends itself well here. For an example problem, let’s calculate the probability distribution of the sum of rolling m 6-sided dice.

type Reducer<T, TResult> = (current: T, accumulator?: TResult) => TResult;  function* nestForLoopRecursive<T, TResult>(   array: T[],   depth: number,   reduce: Reducer<T, TResult> ): Generator<TResult> {   for (const value of array) {     if (depth === 1) {       yield reduce(value);     } else {       for (const next of nestForLoopRecursive(array, depth - 1, reduce)) {         yield reduce(value, next);       }     }   } }  function reduceSum(current: number, prev = 0): number {   return current + prev; }  const pips = [1, 2, 3, 4, 5, 6];  interface RollDistribution {   [key: number]: number; }  function rollMDice(m: number): RollDistribution {   const results: RollDistribution = {};    for (const result of nestForLoopRecursive(pips, m, reduceSum)) {     results[result] = results[result] !== undefined ? results[result] + 1 : 1;   }    return results; }  for (let m = 1; m <= 3; m++) {   console.log(`Rolling $  {m} $  {m === 1 ? 'die' : 'dice'}`);   console.log(rollMDice(m));   console.log(); } 

Output

Rolling 1 die { '1': 1, '2': 1, '3': 1, '4': 1, '5': 1, '6': 1 }  Rolling 2 dice {   '2': 1,   '3': 2,   '4': 3,   '5': 4,   '6': 5,   '7': 6,   '8': 5,   '9': 4,   '10': 3,   '11': 2,   '12': 1 }  Rolling 3 dice {   '3': 1,   '4': 3,   '5': 6,   '6': 10,   '7': 15,   '8': 21,   '9': 25,   '10': 27,   '11': 27,   '12': 25,   '13': 21,   '14': 15,   '15': 10,   '16': 6,   '17': 3,   '18': 1 } 

My understanding is that any recursive function can be rewritten iteratively, though it usually requires some augmentation. (For example, an in-order traversal of a binary tree can be done iteratively if you augment each node with two bits and a parent pointer.)

How can I rewrite nestForLoopRecursive() without using a stack or any other recursive data structure? In particular, is it possible to do this in at most O(n lg(m)) space?

Here’s a CodeSandbox with everything needed written in TypeScript. The code yet to be written starts at line 16. Feel free to answer using whatever language you choose, though, including pseudocode.

Is this an example of Tail Recursion

As I have read in this answer: What is tail recursion? tail recursion is a special case of recursion where the calling function does no more computation after making a recursive call.

Here after the recursive call we just assign the result to another variable. Can we consider this code as tail recursion?

class Solution:     def swapPairs(self, head: ListNode) -> ListNode:          if not head or not head.next:             return head          head_ = head         head_next = head.next         head_next_next = head.next.next          head_next.next = head_          head_.next = self.swapPairs(head_next_next)          return head_next