tail-recursion

How to use tail recursion correctly?

大城市里の小女人 提交于 2020-01-06 18:57:43
问题 I am trying to rewrite this piece of code from https://github.com/lspector/gp/blob/master/src/gp/evolvefn_zip.clj to use recur: (defn random-code [depth] (if (or (zero? depth) (zero? (rand-int 2))) (random-terminal) (let [f (random-function)] (cons f (repeatedly (get function-table f) #(random-code (dec depth))))))) The problem is, I have absolutely no idea how to do that. The only thing I can think of is something like this: (defn random-code [depth] (loop [d depth t 0 c []] (if (or (zero?

print binary numbers in ascending order

巧了我就是萌 提交于 2020-01-06 14:57:11
问题 I was trying to print binary numbers in ascending order of 0's (00 then 01, 10, 11). Such that the zeros are before. I tried using the below code from here but this does not give the right op (running sample) void test2() { final int grayCodeLength = 4; // generate matrix final int grayCodeCount = 1 << grayCodeLength; // = 2 ^ grayCodeLength int grayCodeMatrix[][] = new int[grayCodeCount][grayCodeLength]; for (int i = 0; i < grayCodeCount; i++) { int grayCode = (i >> 1) ^ i; for (int j =0; j

Scheme accumulative recursion with lists

╄→гoц情女王★ 提交于 2020-01-05 06:01:19
问题 How can I pass a list as a parameter to a function adding elements to it recursively,and have it unmodified when it comes out of recursion? I want to use the list at each level of recursion with the list having the values added by deeper recursion levels. To be more specific I want to do a DFS search on a graph and I want to store in the list the nodes I visited. 回答1: One method of doing this is just to return the list so you have access to it at higher levels of recursion. Another method is

Scheme accumulative recursion with lists

陌路散爱 提交于 2020-01-05 05:57:05
问题 How can I pass a list as a parameter to a function adding elements to it recursively,and have it unmodified when it comes out of recursion? I want to use the list at each level of recursion with the list having the values added by deeper recursion levels. To be more specific I want to do a DFS search on a graph and I want to store in the list the nodes I visited. 回答1: One method of doing this is just to return the list so you have access to it at higher levels of recursion. Another method is

Stack overflow exception when using pipes in tail-recursive function

余生长醉 提交于 2020-01-03 07:33:07
问题 I have a naive implementation of a gameloop let gameLoop gamestate = let rec innerLoop prev gamestate = let now = getTicks() let delta = now - prev gamestate |> readInput delta |> update delta |> render delta |> innerLoop delta innerLoop 0L gamestate This implementation throws a stackoverflowexception. In my mind this should be tail recursive. I could make a work around like this let gameLoop gamestate = let rec innerLoop prev gamestate = let now = getTicks() let delta = now - prev let

Can someone describe through code a practical example of backtracking with iteration instead of recursion?

和自甴很熟 提交于 2020-01-03 04:10:41
问题 Recursion makes backtracking easy as it guarantees that you won't go through the same path again. So all ramifications of your path are visited just once. I am trying to convert a backtracking tail-recursive (with accumulators) algorithm to iteration. I heard it is supposed to be easy to convert a perfectly tail -recursive algorithm to iteration. But I am stuck in the backtracking part. Can anyone provide a example through code so myself and others can visualize how backtracking is done? I

Recursive path finding in matrix

北城余情 提交于 2020-01-02 19:37:25
问题 I need to find a path of the number 1 in a matrix[R][C] starting from matrix[0][0] until it gets to matrix[R-1][C-1] , using recursion. I can only go down or right. In most cases, I don't have a problem. My problem is only when there is nowhere to go, and I need to take a step backwards. Per example, this is the matrix I'm getting from a file: 1 0 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 1 1 0 1 1 0 1 1 1 1 0 1 0 1 1 1 The problem is when it gets to matrix[4][0] . I don't understand why it won't

Kotlin recursion stack overflow

假如想象 提交于 2020-01-02 12:41:05
问题 I have written this recursive function in Kotlin: fun recursive(target: String, population: Population, debugFun: (String) -> Unit) : Population { if (population.solution(target).toString() == target) { return population } debugFun.invoke(population.solution(target).toString()) return recursive(target, population.evolve(target), debugFun) } It will run an indeterminate amount of times (because I'm using randomness to converge on solution in evolutionary algorithm). I am frequently getting

How to make a tail-recusive method that can also refer to itself in a non-tail-recursive way

别来无恙 提交于 2020-01-02 08:42:09
问题 Suppose I have a mechanism for long-running computations that can suspend themselves to be resumed later: sealed trait LongRunning[+R]; case class Result[+R](result: R) extends LongRunning[R]; case class Suspend[+R](cont: () => LongRunning[R]) extends LongRunning[R]; The simplest way how to run them is @annotation.tailrec def repeat[R](body: LongRunning[R]): R = body match { case Result(r) => r case Suspend(c) => { // perhaps do some other processing here println("Continuing suspended

Designing tail recursion using java 8

坚强是说给别人听的谎言 提交于 2020-01-01 10:52:04
问题 I was trying out the following example provide in the talk to understand the tail recursion in java8. @FunctionalInterface public interface TailCall<T> { TailCall<T> apply(); default boolean isComplete() { return false; } default T result() { throw new Error("not implemented"); } default T get() { return Stream.iterate(this, TailCall::apply).filter(TailCall::isComplete) .findFirst().get().result(); } } Utility class to use the TailCall public class TailCalls { public static <T> TailCall<T>