recursion

Recursively reverse a linkedlist (code in Stanford CSed library) explanation

…衆ロ難τιáo~ 提交于 2020-01-15 06:09:04
问题 My recursion skill is pretty rusty. I've been thinking about this problem and searched the forum for a long time but still cannot understand. Right now I'm looking at the recursively reverse a linked list code from Stanford CS ed library. #include <stdio.h> struct Node { int x; struct Node *next; }; void Reverse(struct Node ** headRef){ struct Node* first; struct Node* rest; if(*headRef==NULL) return; first= *headRef; rest= first->next; if(rest==NULL) return; Reverse(&rest); printf("Rest%d\n"

Strange result using recursion with while loop

喜你入骨 提交于 2020-01-15 05:09:07
问题 I'm a beginner in Javascript. And while trying recursion by myself, i got some strange result using while loop. And just the right result using If statement. Here's the code and the result : var test = function f(n){ while(n > 0){ document.write(n); f(--n); } }; test(5); And the result : 5432112113211211432112113211211 While using If statement var test = function f(n){ if(n > 0){ document.write(n); f(--n); } }; test(5); The result is : 54321 I can't really debug it in the while case. It gets

Recursively remove a directory using C

假装没事ソ 提交于 2020-01-15 04:55:08
问题 I want to implement this myself and I come up something like this: /* DIR *opendir(const char *name); * * Open a directory stream to argv[1] and make sure * it's a readable and valid (directory) */ if ((dip = opendir(argv[1])) == NULL) { perror("opendir"); return 0; } printf("Directory stream is now open\n"); /* struct dirent *readdir(DIR *dir); * * Read in the files from argv[1] and print */ while ((dit = readdir(dip)) != NULL) { printf("\n%s", dit->d_name); remove(dit->d_name); } I am

Recursion using PHP Simple DOM Parser

最后都变了- 提交于 2020-01-15 03:10:09
问题 For some reason I get recursion while using Simple DOM Parser Library. My HTML is like <div id="root"> <div class="some_div">some text</div> <div class="field_1 misc1"><a href="#">Some text link</a> <strong>15</strong></div> <div class="field_2 misc2"><a href="#">Some text link</a> <strong>25</strong></div> </div> I created PHP script, included single file include_once('simple_html_dom.php'); And I try to get 15 and 25 values from HTML above. But when I run $ret = $html->find('div[id=root]');

Can tail recursion support shorten the stack for other function calls?

大憨熊 提交于 2020-01-14 22:39:06
问题 Can languages that support tail recursion apply the same technique to non-recursive function calls? For example, if the last thing that function foo does is return the value of calling bar , might the language discard foo 's stack frame? Are any languages known to actually do this? 回答1: Yes it can. For example, consider the following C code: int f(); int g() { return f(); } When I compile this using gcc 4.6.3 with -O3 on x86-64 , I get the following assembly for g() : g: xorl %eax, %eax jmp f

Rudimentary Tree, and Pointers, in Rust

喜夏-厌秋 提交于 2020-01-14 18:50:31
问题 Coming from a scripting language background with some C, trying to 'learn' Rust leads me to question my competence. I'm trying to figure out how to change an owned pointer, and struggling to do it. Besides copying in from the extra libs, I can't figure out the recursion I need on a binary tree. Particularly, I don't know how to swap out the pointer branches. Whereas with a linked list I can cheat and use a temporary vector to return a new list, or prepend a new Cons(value, ~Cons) to the list

Rudimentary Tree, and Pointers, in Rust

女生的网名这么多〃 提交于 2020-01-14 18:49:27
问题 Coming from a scripting language background with some C, trying to 'learn' Rust leads me to question my competence. I'm trying to figure out how to change an owned pointer, and struggling to do it. Besides copying in from the extra libs, I can't figure out the recursion I need on a binary tree. Particularly, I don't know how to swap out the pointer branches. Whereas with a linked list I can cheat and use a temporary vector to return a new list, or prepend a new Cons(value, ~Cons) to the list

Operations on sublists

时间秒杀一切 提交于 2020-01-14 15:55:14
问题 I am currently wondering on an approach to split a list in sub-lists according to a given criteria. Because of the didactic purpose of this work, I do not use built-in functions. IE, the following program should, given a list, return a list of lists, where each sub-list does not have duplicates and is in ascending order: increment [4;4;10;20;5;30;6;10] = [[4;10;20];[5;30];[6;10]] increment [5;6;4;3;2;1] = [[5;6];[4];[3];[2];[1]] My best attempt so far is based on this chunk of code I produced

How can this function be written using foldr?

放肆的年华 提交于 2020-01-14 14:07:41
问题 I have this simple function which returns a list of pairs with the adjacents elements of a list. adjacents :: [a] -> [(a,a)] adjacents (x:y:xs) = [(x,y)] ++ adjacents (y:xs) adjacents (x:xs) = [] I'm having problems trying to write adjacents using foldr . I've done some research but nothing seems to give me a hint. How can it be done? 回答1: Tricky folds like this one can often be solved by having the fold build up a function rather than try to build the result directly. adjacents :: [a] -> [(a

Recursive Function in matlab

喜你入骨 提交于 2020-01-14 14:01:26
问题 How do I write a recursive Function in matlab, it basically being a Markov chain! I tried writing a pseudo code for it and new to MATLAB : The function goes this way: P= Probability x= status(0,1) Dij= probability to pick a site P(Status of Site(i) being x at next time step)= Summation[P(Status of Site(i) being x at previous time step)*Dij] I have tried code, can anyone tel me whether its right: function [t,index]= CopyingInfluenceModel %%Define constants StatusP1=rand(1,0); StatusP0=rand(1,0