recursion

Is there a more modern, OO version of "Let's Build a Compiler"?

Is there a more modern, maybe object-oriented, equivalent to Jack Crenshaw's "Let's Build a Compiler" series? A while back I stumbled across "Let's Build a Compiler" and could just not resist writing some code. I wrote a recursive-descent C compiler in C# that output .NET CIL. "Write once, leak everywhere" was my slogan. Too bad I di...

Trying to make a recursive call in C++

Hello Stack Overflow! This is my first question here so be kind :-) I'm trying to make a recursive call here, but I get the following compiler error: In file included from hw2.cpp:11: number.h: In member function ‘std::string Number::get_bin()’: number.h:60: error: no matching function for call to ‘Number::get_bin(int&)’ number.h:27: no...

Is my Fibonacci subroutine an example of recursion in Perl?

As we all know that we can send any number of arguments to a subroutine in Perl. Would the following example be a correct demonstration of recursion to display fibonacci series (5 values)? #!/usr/bin/perl -w use strict; sub recursion { if ($_[0] && $_[2]) { print $_[2],"\n"; if ($_[0] < 5) { return recursion($_[...

At a glance can anyone tell me why this Action is so slow?

This thing was not noticed to be slow at first because there were not many 'tree nodes' in the database. Now it's really slow, at a glance is there anything major that is wrong with this? I really need to optimize it and before I rework the entire thing I was wondering if anything stands out as being the real pain point. I have narrowed ...

Best way to recursively merge array without overwriting duplicates keys

I have a multidimensional array of form data thats an been unserialized from YAML. As such it looks something like this: Array( 'name' => 'Somone', 'email' => '[email protected]', 'billing' => Array( 'address_1' => '1234 Somewhere' 'address_2' => NULL, 'city' => 'Somewhere', 'state' => 'ST' 'country' => '...

What's the reason of marking a recursive function as rec in F#?

I am not sure if this is a stupid question but I was going through the tutorial that comes with VS 2010 and there is a function like this: let rec factorial n = if n=0 then 1 else n * factorial (n-1) What's the reason of this recursive function to be marked with the rec keyword? Is it so that the compiler is assured of it being recur...

Finding stack overflow exceptions in a windows service

I have inherited a large and complex C# windows service project that crashes every now and then. The logging system is not logging any messages which I initially thought strange but I now understand that logging might fail if there's a stack overflow or out-of-memory exception. So one of the tasks that I have is to try and find any recu...

Crockford's hanoi function (from "The Good Parts")

Hi, at the moment I'm reading Douglas Crockford's book, and the towers of hanoi function is a bit over my head. Even with logging stuff to the console I wasn't able to really understand what's going on. Here's the function with my additions: var hanoi = function (disc, src, aux, dst) { console.log(disc); console.log(src, dst); ...

C# - Recursive Function Issue.

Here's my function: static Map AddFormation(Map _map, Tile tile, int x, int y, int length, Random rand, Tile endTile = (Tile)Int32.MaxValue) { //so a call to AddFormation without the endTile will work, if I don't want a border. if ((int)endTile == Int32.MaxValue) endTile = tile; if (x >= 0 && x < _map.Data.GetLength(0)...

Non recursive depth first search on graphs for Delphi

I am searching for a non-recursive depth first search algorithm on graphs in Pascal (Delphi). I need DFS for computing strongly or bi-connected components of large graphs. Currently I am using a recursive variant of the algorithm: http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm The problem is that for ...

Stackoverflow error in this recursion

What's wrong with this recursion in Java? public class findPyt { public static int sum = 0; public static void main(String[] args) { findP(3, 4, 5); } public static void findP(int a, int b, int c) { sum = a+b+c; if (sum == 1000) { System.out.println("The Triplets are:...

Writing a recurrence relation for a method

I have a bit of code and need to write a recurrence relation for it. The code simply calculates 2 raised to the nth power. Any help is appreciated. public static int two(int n) { if (n==0) { return 1; } else if (n%2 == 0) { int x = two(n/2); return x*x; } else { return 2 * two(n-1) } ...

Recursive stored functions in MySQL

I'm trying to make a function that recursively builds a path for a specific category CREATE FUNCTION getPath(inId INT) RETURNS TEXT DETERMINISTIC BEGIN DECLARE return_path TEXT; DECLARE return_parent_id INT; SELECT CONCAT('/', name) INTO return_path FROM article_categories WHERE id = inId; SELECT parent_id INTO return_pa...

Will I need a recursive function to iterate through a Dictionary<String, Object> of Dictionary<String, Object>s?

And there could be many levels, who knows how deep it could go! Also let's say for this specific question that the String for another Dictionary will be "Dictionary" and a value that I want to access/modify will be "Data". ...

What's the alternative to exceptions in a deep Haskell recursion?

I'm trying to learn Haskell by writing little programs... so I'm currently writing a lexer/parser for simple expressions. (Yes I could use Alex/Happy... but I want to learn the core language first). My parser is essentially a set of recursive functions that build a Tree. In the case of syntax errors, I'd normally throw an exception (i.e...

Is there a way to tell if I'm using recursion in Python?

I'm writing a function to traverse the user's file system and create a tree representing that directory (the tree is really a TreeView widget in Tkinter, but that's functionally a tree). The best way I can think of doing this is recursion. However, one of my cases in the function requires me to know if it is the "original" function call...

how to recursively build treeview in c#

private void enumerateValues(IEnumerator<KeyValuePair<string, object>> iEnumerator, TreeNode parentnode) { if(iEnumerator is IEnumerable) { // ADD THE KEY TreeNode childNode = parentnode.Nodes.Add(iEnumerator.Current.Key); enumerateValues(iEnumerator.Current.Value, childNode); } else { /// add the value...

how to do this java recursive

I have this code: static int countStu = 0; public static int countStudent(Node<Student> lst) { // pre : true // post : res = number of students in list if (lst != null) { countStu++; countStudent(lst.getNext()); } return countStu; } The problem with this method is I must declare countStu outs...

Binary Search to Compute Square root (Java)

I need help writing a program that uses binary search to recursively compute a square root (rounded down to the nearest integer) of an input non-negative integer. This is what I have so far: import java.util.Scanner; public class Sqrt { public static void main(String[] args) { Scanner console = new Scanner(System.in); Sys...

C recursive header file inclusion problem?

Suppose you have to related structures defined in 2 header files like below: a.h contents: #include b.h typedef struct A { B *b; } A; b.h contents: #include a.h typedef struct B { A *a; } B; In such this case, this recursive inclusion is a problem, but 2 structures must point to other structure, how to accomplish this? ...