recursion

Why do I not need to explicitly lend a borrowed, mutable variable?

微笑、不失礼 提交于 2019-12-30 17:43:52
问题 I've just written a small Rust program which calculates Fibonacci numbers and memoizes the calculation. It works, but I'm a little confused about why, especially the recursive call. (It also probably isn't idiomatic.) Here's the program: use std::collections::HashMap; fn main() { let n = 42; // hardcoded for simplicity let mut cache = HashMap::new(); let answer = fib(n, &mut cache); println!("fib of {} is {}", n, answer); } fn fib(n: i32, cache: &mut HashMap<i32,i32>) -> i32 { if cache

Tail Call Optimisation in Java

旧街凉风 提交于 2019-12-30 17:24:08
问题 As of Java 8 , Java does not provide Tail-Call Optimization (TCO). On researching about it, I came to know the reason which is: in jdk classes [...] there are a number of security sensitive methods that rely on counting stack frames between jdk library code and calling code to figure out who's calling them. However Scala , which is based on JVM has support for Tail-Call Optimisation. Scala does tail recursion optimisation at compile-time . Why can't Java use the same approach ? PS: Not sure

Is there a way to make a class recursive?

两盒软妹~` 提交于 2019-12-30 14:54:30
问题 So I would like to create a class that can have an object which type is itself. Something like this: class foo { foo Avalue = foo(); foo Bvalue = foo(); foo(int a, int b) { Avalue = goo(a); Bvalue = goo(b); } foo(foo a, int b) { Avalue = foo(a); Bvalue = goo(b); } foo(foo a, foo b) { Avalue = foo(a); Bvalue = foo(b); } } class goo : foo { int value; } so that I can have a recursive object that always terminates at "goo" objects. Is there a way to make it? 回答1: No. That is fundamentally

How does Eloquent JavaScript recursion example terminate as return 1 but still output exponential value

二次信任 提交于 2019-12-30 12:48:53
问题 In the code below I understand exactly how it works, right up until the termination and the 'return 1'. I would assume that once the function terminates it should output 1 but instead it does what you would expect an exponential program to do, it outputs the correct answer which in this case is 9. My question is why is this so ? I assume the way I visualize recursion is incorrect ( maybe my idea of how the stack treats recursion ?) I found a similar question to mine here but it doesn't help

How does return statement with recursion calls hold intermediate values in Python?

喜你入骨 提交于 2019-12-30 11:21:34
问题 Was reviewing some python code related to recursion calls and noticed the return statement looked interesting. How does the recursion work when there is no variable assignment in the return statement for the next recursive call? As the recursive calls were being made, the intermediate summed values weren't getting stored in an obvious place. The debugger seems to show the list reducing by one each call, but I just don't understand where the intermediate values are getting stored. Another

Can every recursion be changed to iteration?

时光毁灭记忆、已成空白 提交于 2019-12-30 10:44:41
问题 Is every recursive function convertible to iteration? What characteristic should a recursive function have in order for it to be implemented using iteration? I've been trying to define the following function using iteration but seems like a no-go! It is supposed to explore all the paths (nodes) in a maze. Can anyone rewrite this using iterations? If it is not possible, why not? typedef int[0,99] id_t; bool visited[id_t]; int path[id_t]; int pathCounter = 0; struct { id_t id; bool free; int

Recursion in place of multiple nested for loops?

允我心安 提交于 2019-12-30 10:03:15
问题 Im having some issues with trying to update a nested for loop to use recursion instead. Is it possible to access the a,b and c variables from the earlier for loops when using recursion? Below is a simple example of what im trying to convert into a recursive call. for(int a= 0; a < 10; a++) { for(int b = 0; b < 20; b++) { for(int c = 0; c < 10; c++) { int[] indexes = new int[3]{a,b,c} collection.add(indexes); } } } EDIT: The solution needs to be able to be adjusted at runtime, such that a user

Recursion in place of multiple nested for loops?

天大地大妈咪最大 提交于 2019-12-30 10:02:42
问题 Im having some issues with trying to update a nested for loop to use recursion instead. Is it possible to access the a,b and c variables from the earlier for loops when using recursion? Below is a simple example of what im trying to convert into a recursive call. for(int a= 0; a < 10; a++) { for(int b = 0; b < 20; b++) { for(int c = 0; c < 10; c++) { int[] indexes = new int[3]{a,b,c} collection.add(indexes); } } } EDIT: The solution needs to be able to be adjusted at runtime, such that a user

Recursively printing data structures in Perl

筅森魡賤 提交于 2019-12-30 09:50:14
问题 I am currently learning Perl. I have Perl hash that contains references to hashes and arrays. The hashes and arrays may in turn contain references to other hashes/arrays. I wrote a subroutine to parse the hash recursively and print them with proper indentation. Though the routine works as expected, my instructor was not convinced about the readability and elegance of the below code. I would really appreciate to get the views of Perl experts here on possible optimization of the below code.

Crockford's hanoi function (from “The Good Parts”) [duplicate]

不打扰是莪最后的温柔 提交于 2019-12-30 09:33:10
问题 This question already has answers here : How does recursive algorithm work for Towers of Hanoi? (2 answers) Closed 5 years ago . 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); if (disc > 0) { hanoi(disc - 1, src, dst,