Tags "recursion" Result


Can someone explain me this >>def f(l): if l == []: return [] else: return f(l[1:]) l[:1] # <= cant figure this, how is all sum at the end? thanks! Solution Gigs_ wrote: Can someone explain me this def f(l): if l == []: return [] else: return f(l[1:]) l[:1] # <= cant figure this, how is

it1352 0 2019-06-25

tail recursion vs. forward recursion

Can someone give me the difference between these two kinds recursions and example (specifically in OCaml)? Solution A tail recursive function is a function where the only recursive call is the last one in the function. A non-tail recursive function is a function where that is not the case. A backward recursion is a recursion where in each recursi

it1352 17 2019-05-17

how to search elements in a 2d array using recursion

I have a 2d char array and I'm trying to find a specific character using recursion. public class Test { char arry [][] = {{'1',' ','B'}, {'C','K','M'}, {'H','R','P'} }; public Test(){ recursion(0,0,arry[0][0]); } private void recursion(int row, int col, char c) { if(c==' '){ System.out.print("

it1352 3 2019-05-08

Convert normal recursion to tail recursion

I was wondering if there is some general method to convert a "normal" recursion with foo(...) foo(...) as the last call to a tail-recursion. For example (scala): def pascal(c: Int, r: Int): Int = { if (c == 0 || c == r) 1 else pascal(c - 1, r - 1) pascal(c, r - 1) } A general solution for functional languages to convert recursive functi

it1352 0 2020-07-02

Is recursion ever faster than looping?

I know that recursion is sometimes a lot cleaner than looping, and I'm not asking anything about when I should use recursion over iteration, I know there are lots of questions about that already. What I'm asking is, is recursion ever faster than a loop? To me it seems like, you would always be able to refine a loop and get it to perform more quick

it1352 5 2019-05-21

recursion using only heap area

Are there examples of recursion using only heap area? Solution In C, function call-based recursion always uses the stack, almost by definition. If you are willing to convert your recursion to iteration, then it is possible to use only heap space, but that isn't really recursion. You would do so by implementing a stack in the heap. Certain probl

it1352 3 2019-05-10

Removing Left Recursion in ANTLR

As is explained in Removing left recursion , there are two ways to remove the left recursion. Modify the original grammar to remove the left recursion using some procedure Write the grammar originally not to have the left recursion What people normally use for removing (not having) the left recursion with ANTLR? I've used flex/bison for parser,

it1352 1 2020-09-02


Why the sys module does not have a function like getrecursiondepth? It is very easy to implement: #include "Python.h" static PyObject * getrecursiondepth(PyObject *self, PyObject *args) { PyThreadState *tstate; if (!PyArg_ParseTuple(args, ":recursion_depth")) return NULL; tstate = PyThreadState_GET(); return Py_BuildValue(&quo

it1352 3 2019-06-25

"Necessary" Uses of Recursion in Imperative Languages

I've recently seen in a couple of different places comments along the lines of, "I learned about recursion in school, but have never used it or felt the need for it since then." (Recursion seems to be a popular example of "book learning" amongst a certain group of programmers.) Well, it's true that in imperative languages such as Java and Ruby[1],

it1352 3 2019-05-21