recursion

tail recursion sum, power, gcd in prolog?

hi guys, how can I accomplish this: Give a tail-recursive definition for each of the following predicates. power(X,Y,Z): XY=Z. gcd(X,Y,Z): The greatest common divisor of X and Y is Z. sum(L,Sum): Sum is the sum of the elements in L. so far I have done this but not sure if that's correct power(_,0,1) :- !. power(X,Y,Z) :- Y1 is Y -...

authlogic crashes with infinite recursion

Hello, I have some trouble with using authlogic in my rails app, so I began using the blank example from github.com/binarylogic/authlogic_example which doesn't work either. I spent a day installing ruby 1.9.1 and 1.8 and jruby1.8, neither did work. The fun thing is that another rails app worked on my server. That said, I just cannot se...

ocaml recurssion iteration- find last occurence of element in a list

Assuming l is a List and elem is an element, how can I return the last occurence of the element elem in the list l? Also return -1 if the element does not exisit in l. I dont quite understand how to use recursion for iterating through the list... let rec getLastOccurence l elem = ;; ...

XSLT conditional node selection

Here is my problem. I have a "menu" organized like this: <menutree> <menuitem name="Foo"> <menuitem name="Sub-Foo"> <menuitem name="Sub-sub-foo1"/> <menuitem name="Sub-sub-foo2"/> <menuitem name="Sub-sub-foo3"/> </menuitem> <menuitem name="Other-Sub-Foo"> <menuitem name="Other-Sub-sub-foo1"/> ...

Break string into separate lines: a showdown

Accommodating legacy database tables that were designed specifically for the IBM mainframe screens they represent has caused me much aggravation. Often, I find the need to break a string into multiple lines to fit the table column width as well as the users who are still viewing the data with terminal emulators. Here are two functions I'...

How does the Jinja2 "recursive" tag actually work?

I'm trying to write a very simple, tree-walking template in jinja2, using some custom objects with overloaded special methods (getattr, getitem, etc) It seems straightforward, and the equivalent python walk of the tree works fine, but there's something about the way that Jinja's recursion works that I don't understand. The code is show...

Lisp Recursion Not Calling Previous Functions

Hi, I'm trying to have a function compare the first argument of a passed in argument to a value, then if it is true, perform some function, then recursively call the same function. (defun function (expression) (cond ((equal (first expression) "+") (progn (print "addition") (function (rest expression)))))) For so...

constructing random "integer" tree - depth-first / breadth-first

Hi, This is my first ever attempt at writing a recursive function in c. The following-code works. I apologize for the long posting, but I am trying to be as clear as possible. I am trying to generate a tree where each node (inode) has an integer field "n". Correspondingly, each inode has an array of pointers to "n" other inodes. Func...

Reducing values in one table until reserves depleted in another - recursion?

Hello everyone, I have two tables - let's call them dbo.ValuesToReduce and dbo.Reserve The data in the first table (dbo.ValuesToReduce) is: ValuesToReduceId | PartnerId | Value ------------------------------------- 1 | 1 | 53.15 2 | 2 | 601.98 3 | 1 | 91.05 4 ...

Recursive chmod in ruby on mac OS X?

I need to delete an application (MyApp.app), which has read only permissions in all its enclosed folders. Before I delete it, I should change the permissions on all enclosed files/directories to 0644. How do I do this recursively? I've tried begin; FileUtils.chmod(0644, '#{config.appPath}'); rescue; end begin; FileUtils.rm_r('#{config....

Copying a Directory Tree File by File

My advisor frequently compiles a bunch of software into his /usr/local and then distributes his /usr/local as a tarball. I then extract the tarball onto our lab machines for our use. Until now, this has worked fine, we just replace the old /usr/local with the contents of the tarball. But I have started installing my own software on thes...

What does this recursive snippet of Java code do?

I passed through this recursion example, but I couldn't find out its purpose. Could you please help me? public static int somerec(int n, int x, int y) { if (n < x*y) return 0; else if (n == x*y) return 1; else { int sum = 0; for (int i = x; i <= n;i++) sum += somerec(n-i, i, y-1);...

Infinitely Deep Recursive Children

I have a single-table with the following (relative) structure: foo_id, parent_foo_id, foo_name I would like to build an (effectively) infinite-depth recursive array with these entities, and consequently output them into a tree menu. Nodes with no "parent_foo_id" would be considered at the top of the heirerchy, while all successive n...

Are there any cases when I would want to use an explicit stack over recursion?

Are there any cases when I would want to use an explicit stack data-structure in my algorithms, as opposed to doing recursion (which uses the call stack)? Is there any benefit to doing it one way over the other? I would think using the explicit data-structure would be more performant because it doesn't require the method calls but then...

Java Need help implementing an algorithm

This algorithm is so advanced for my basic programming skills that I just don't see how I could implement it. I'm posting this in a new question because I can't keep bothering the guy who gave me the algorithm alone about this in the comment section in the previous question. MaxSet(node) = 1 if "node" is a leaf MaxSet(node) = Max(1 + S...

NHibernate many-to-one loading alternative?

I have a Parent/Child object/mapping as follows: class Parent { int Id; string name; List children; } <bag name="Children" cascade="all" lazy="false "> <key column="ParentId" /> <one-to-many class="Child" /> </bag> class Child { int Id; Parent Parent; string Name; } <many-to-one name="Parent" column="ParentId" /> O...

Ocaml - Iterative to Recursion

For an assignment, i have written the following code in recursion. It takes a list of a vector data type, and a vector and calcuates to closeness of the two vectors. This method works fine, but i dont know how to do the recursive version. let romulus_iter (x:vector list ) (vec:vector) = let vector_close_hash = Hashtbl.create 10 in...

How to delete empty subfolders with PowerShell

I have a share that is a "junk drawer" for end-users. They are able to create folders and subfolders as they see fit. I need to implement a script to delete files created more than 31 days old. I have that started with Powershell. I need to follow up the file deletion script by deleting subfolders that are now empty. Because of the ...

Catching "Stack Overflow" exceptions in recursive C++ functions

Is it possible to catch a stack overflow exception in a recursive C++ function? If so, how? so what will happen in this case void doWork() { try() { doWork(); } catch( ... ) { doWork(); } } I am not looking for an answer to specific OS. Just in general ...

Java recursive binary tree

Welcome! I have a recursive public static method named less that takes a tree node (an original binary tree, not really a search tree) and a int parameter that returns if all the values in the tree are less than the integer. So, I would use a public class TN { public int value; public TN left, right; public TN(int v, TN l, TN r) {v...