tail-recursion

How do I implement Tail recursion with my Fibonacci method?

岁酱吖の 提交于 2020-01-30 08:23:07
问题 I'm trying to compute large numbers of the Fibonacci sequence, hence why I am using big integer. I can get up to about 10000 the way it is, but I run out of stack space. I realize I can increase stack and heap space, but it is my understanding that tail recursion can get around the space issue. Here is my code.. public class FibRecursion{ static BigInteger[] fval; public static void main(String[] args) { int index; Scanner input = new Scanner(System.in); index = input.nextInt(); fval = new

Erlang, finding the number of occurrences of a number in a list

好久不见. 提交于 2020-01-25 04:18:11
问题 I am new to Erlang and trying to write a program that will take a list of numbers like this [1,5,4,5,3,2,2,8,11] as an input parameter of a function. The function should return a list of tuples that records the number and it's number of appearances in the list like so. [{1,1},{2,2},{3,1},{4,1},{5,1},{8,1},{11,1}]. I have the following code list([]) -> []; list([First | Rest]) -> [{First,+1} | list(Rest)]. But I dont understand how it is that I can do the counting operation? Thanks 回答1: See

How to force a function to exit after calling another function?

人走茶凉 提交于 2020-01-15 10:18:27
问题 I have two functions each calling the other on their final line as a means of passing control. Is there an elegant way to force one function to exit before the other gets executed, so that you don't stack your program up on corpses? Example: def f1(x): print("f1",x) f2(x+1) def f2(x): print("f2",x) f1(x+1) f1(0) The way it is now, after ~1000 calls it runs into the recursion limit. Of cause, a simple external control structure would get rid of the problem, but I'd rather have the control flow

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

F# using accumulator, still getting stack overflow exception

十年热恋 提交于 2020-01-14 18:49:53
问题 In the following function, I've attempted to set up tail recursion via the usage of an accumulator. However, I'm getting stack overflow exceptions which leads me to believe that the way I'm setting up my function is't enabling tail recursion correctly. //F# attempting to make a tail recursive call via accumulator let rec calc acc startNum = match startNum with | d when d = 1 -> List.rev (d::acc) | e when e%2 = 0 -> calc (e::acc) (e/2) | _ -> calc (startNum::acc) (startNum * 3 + 1) It is my

Exit Recur Loop in Clojure

旧街凉风 提交于 2020-01-13 19:01:11
问题 I'd like break out of the below loop and return the best-min-move when line 10 evaluates to true. I've looked at the output with print statements and when line 10 does evaluate to true, it finds the data that I'm looking for but continues to recur. In Clojure is there a way to stop the loop when a statement evaluates to true? Or should I be using something other than a loop recur? (defn minimax [board max-mark min-mark depth best-score] (loop [board board max-mark max-mark min-mark min-mark

Can a convolution function written in tail recursive form?

旧巷老猫 提交于 2020-01-13 18:23:05
问题 I have a function that I want to write in tail recursive form. The function calculates the number of ways to get the sum of k by rolling an s sided die n times. I have seen the mathematical solution for this function on this answer. It is as follows: My reference recursive implementation in R is: sum_ways <- function(n_times, k_sum, s_side) { if (k_sum < n_times || k_sum > n_times * s_side) { return(0) } else if (n_times == 1) { return(1) } else { sigma_values <- sapply( 1:s_side, function(j)

What is the algorithm of thinking recursive? (on the specific example)

南楼画角 提交于 2020-01-13 10:14:08
问题 I just can't wrap my head around recursion. I understand all of the concepts (breaking solution into smaller cases) and I can understand solutions after I read them over and over again. But I can never figure out how to use recursion to solve a problem. Is there any systematic way to come up with a recursive solution? Can someone please explain to me their thought process when they try to solve the following recursive problem: "Return all permutations of a string using recursion" . Here is an

Can a backtracking tail recursive algorithm be converted to iteration?

六眼飞鱼酱① 提交于 2020-01-12 01:59:16
问题 Let's take the Knight Tour problem. Can that be converted to iteration? What is confunsing me is the backtracking part. How do I backtrack in a loop? Do I have to necessarily use a stack data-structure to implement backtracking when I go from recursion to iteration? I asked this question in a better way here: Can someone describe through code a practical example of backtracking with iteration instead of recursion? 回答1: No, it can't be. All recursive algorithms can be implemented iteratively,

Counting occurences of characters in a String using tail recursion

孤街浪徒 提交于 2020-01-07 09:42:56
问题 I have no clue how to count the occurrences of characters in a string using tail recursion in scala. I need to run a program with input times(explanation) and the output is: List((e,1), (x,1), (p,1), (l,1), (a,2), (n,2), (t,1), (i,1), (o,1)) I tried running RLE so far but the topic of tail recursion is new to me, so some steps/algorithm for doing so would be perfect 回答1: Possible Solutions: A String is a list of characters. Group them by identity which is (x => x), then count them. Normally