recursion

how to solve "stack space overflow" in haskell

Hi! Running the following program will print "space overflow: current size 8388608 bytes." I have read this and this, but still don't know how to resolve my problem. I am using foldr, shouldn't it be guaranteed to be "tail recursive"? I feel great about Haskell so far until I know I should prevent "space overflow" when using the powe...

How can I replace multiple foreach loops with a 'for' loop or a recursive function?

Hello: I am stuck with a bunch of foreach loops and would like to know if there is a way to hone them down to a simple 'for' loop or a recursive function? I'm trying to generate HTML with the elements nested inside each other. I guess what I'm trying to get at is an arrays of arrays. But I don't know how to move forward with what I've c...

php infinite loop

Hello, This function gives me an infinite loop function getCats($parent,$level){ // retrieve all children of $parent $result = ""; $query = "SELECT title,parent_id from t_cats where parent_id = '$parent'"; if($rs = C_DB::fetchRecordset($query)){ while($row = C_DB::fetchRow($rs)){ $result .= str_repeat($paren...

Return Random Number but not 2

Why is it this sometimes returns 2? function pickServer(){ $varr = rand(1,4); if($varr==2){ pickServer(); } return $varr; } ...

Simulating nested loops

In a beginner's programming book (free licence) there was the following code, dynamically creating nested loops in Java: import java.util.Scanner; public class RecursiveNestedLoops { public static int numberOfLoops; public static int numberOfIterations; public static int[] loops; public static void main(String[] args) { S...

Help with Creating a Recursive Function C#

I am creating a forecasting application that will run simulations for various "modes" that a production plant is able to run. The plant can run in one mode per day, so I am writing a function that will add up the different modes chosen each day that best maximize the plant’s output and best aligns with the sales forecast numbers provide...

To grep efficiently by upward recursion

How can you run the following in Grep? grep "TODO" * grep "TODO" */* grep "TODO" */*/* grep "TODO" */*/*/* grep "TODO" */*/*/*/* I run unsuccessfully grep -r "TODO" I get what I want by ack-grep by ack-grep TODO. ...

Recursive function with static variable

I have a recursive function with a static variable "count". The function increments count recursively and since it has file scope, when I call foo() a second time, count is still equal to 5. Is there a technique to reset count to 0 before the second time foo() is called? Basically, I don't want count to have file scope but I want it to ...

Recursively creating an indexbar (UL, LI based)

I'm using awesome_nested_set for my central website structure. I want to be able to build a UL/LI based indexbar off of the nested set but also want it to take advantage of the low database intensiveness of nested set. When I was using acts_as_tree I had a recursive function that built the indexbar. It just called itself if it encountere...

How can I find all permutations of a string without using recursion?

Can someone help me with this: This is a program to find all the permutations of a string of any length. Need a non-recursive form of the same. ( a C language implementation is preferred) using namespace std; string swtch(string topermute, int x, int y) { string newstring = topermute; newstring[x] = newstring[y]; newstring[y] = t...

prototype recursive $

I'm using prototype and have some divs with id p1 p2 p3... In these divs I have some divs with id d1 d2 d3... I would like to use $ to get the specified d element, in the specified p element. If I'm given the values 3 and 4 I should be able to go $('p3').$('d4') This syntax doesn't work of course. There's an easy way to do this with ...

Can I add the same DataRow to a DataTable multiple times?

If I have a DataTable and want to create a new row I first call DataTable.NewRow() which returns a DataRow. This row then has the schema of the Table, and I can set the value of each field in the row. At this point does the Table "know" about the Row, or does that only happen after I call DataTable.Rows.Add(DataRow row)? If I call D**at...

Reaching end of list in prolog

Hi, I've been given the question: 'Define a predcate ordered/1, which cheks if a list of integers is correctly in ascending order. For example, the goal ordered([1,3,7,11]) should succed, as should the goal ordered ([1,3,3,7]), whereas the goal ordered([1,7,3,9]) should fail. So far I have this: ordered([]). ordered([N, M|Ns]):- ap...

How to [recursively] Zip a directory in PHP?

Directory is something like: home/ file1.html file2.html Another_Dir/ file8.html Sub_Dir/ file19.html I am using the same PHP Zip class used in PHPMyAdmin http://trac.seagullproject.org/browser/branches/0.6-bugfix/lib/other/Zip.php . I'm not sure how to zip a directory rather than just a file. Here's what I hav...

Summation notation in Haskell

On the Wikipedia page about summation it says that the equivalent operation in Haskell is to use foldl. My question is: Is there any reason why it says to use this instead of sum? Is one more 'purist' than the other, or is there no real difference? ...

How to make this method non-recursive?

Hey. This example is pretty specific but I think it could apply to a broad range of functions. It's taken from some online programming contest. There is a game with a simple winning condition. Draw is not possible. Game cannot go on forever because every move takes you closer to the terminating condition. The function should, given a st...

Convert string to integer recursively?

Here is a simple function that converts a string to an integer. int str2int(char *str) { int ret = 0; char *c; for (c = str; (*c != '\0') && isdigit(*c); ++c) ret = ret*10 + *c - '0'; return ret; } As an exercise, I'd like to write a recursive function that does the same thing. This is what I came up with. ...

How to determine the height of a recursion tree from a recurrence relation?

How does one go about determining the height of a recursion tree, built when dealing with recurrence run-times? How does it differ from determining the height of a regular tree? edit: sorry, i meant to add how to get the height of the recursion tree from the recurrence relation. ...

Hierarchial SQL Query

Hi, I have a self-referencing table (Customers) and a table that will link to one record in that table (Companies) i.e. Customers Companies ********* ********* ID ID ManagerID --> DirectorID ManagerID refers to another record in the Customer table. I need to perform a query where by given a specific customer I...

How does this C code work?

I was looking at the following code I came across for printing a string in reverse order in C using recursion: void ReversePrint(char *str) { //line 1 if(*str) { //line 2 ReversePrint(str+1); //line 3 putchar(*str); //line 4 } } I am relatively new to C and am confused by line 2. *str fr...