tail-recursion

Explanation of lists:fold function

放肆的年华 提交于 2019-12-29 09:05:13
问题 I learning more and more about Erlang language and have recently faced some problem. I read about foldl(Fun, Acc0, List) -> Acc1 function. I used learnyousomeerlang.com tutorial and there was an example (example is about Reverse Polish Notation Calculator in Erlang): %function that deletes all whitspaces and also execute rpn(L) when is_list(L) -> [Res] = lists:foldl(fun rpn/2, [], string:tokens(L," ")), Res. %function that converts string to integer or floating poitn value read(N) -> case

Convert normal recursion to tail recursion

纵然是瞬间 提交于 2019-12-29 03:15:10
问题 I was wondering if there is some general method to convert a "normal" recursion with foo(...) + foo(...) as the last call to a tail-recursion. For example (scala): def pascal(c: Int, r: Int): Int = { if (c == 0 || c == r) 1 else pascal(c - 1, r - 1) + pascal(c, r - 1) } A general solution for functional languages to convert recursive function to a tail-call equivalent: A simple way is to wrap the non tail-recursive function in the Trampoline monad. def pascalM(c: Int, r: Int): Trampoline[Int]

is erlangs recursive functions not just a goto?

醉酒当歌 提交于 2019-12-28 15:26:10
问题 Just to get it straight in my head. Consider this example bit of Erlang code: test() -> receive {From, whatever} -> %% do something test(); {From, somethingelse} -> %% do something else test(); end. Isn't the test() call, just a goto? I ask this because in C we learned, if you do a function call, the return location is always put on the stack. I can't imagine this must be the case in Erlang here since this would result in a stackoverflow. In basic. We had 2 different ways of calling functions

Converting a recursive function to tail recursive

雨燕双飞 提交于 2019-12-25 18:55:49
问题 I have the following recursive function to count all the nodes having value 20, in a circular doubly linked list. I need to convert this to tail recursive function to prevent safety issues. Please help me with the same. Thanks int count(node *start) { return count_helper(start, start); } int count_helper(node *current, node *start) { int c; c = 0; if(current == NULL) return 0; if((current->roll_no) == 20) c = 1; if(current->next == start) return c; return (c + count_helper(current->next,

Why does the absence of an else block translate to Unit type return for a function?

▼魔方 西西 提交于 2019-12-25 18:52:20
问题 I noticed there is a type mismatch caused in the line else if(r1 == 0 || divisors.tail.isEmpty || !divisors.tail.contains(r1)){newAcc} . Because there is no else clause to my if ... else if ... def euclidianDivision(dividend:Int,divisor:Int):(Int,Int)={ val quotient = dividend/divisor val remainder = dividend%divisor (quotient,remainder) } def firstExpansion(dividend:Int,divisors:List[Int]):List[(Int,Int)]={ def firstExpansionIter(dividend:Int,divisors:List[Int], acc:List[(Int,Int)]):List[

clojure - contains?, conj and recur

怎甘沉沦 提交于 2019-12-25 15:23:10
问题 I'm trying to write a function with recur that cut the sequence as soon as it encounters a repetition ( [1 2 3 1 4] should return [1 2 3] ), this is my function: (defn cut-at-repetition [a-seq] (loop[[head & tail] a-seq, coll '()] (if (empty? head) coll (if (contains? coll head) coll (recur (rest tail) (conj coll head)))))) The first problem is with the contains? that throws an exception, I tried replacing it with some but with no success. The second problem is in the recur part which will

clojure - contains?, conj and recur

烂漫一生 提交于 2019-12-25 15:21:22
问题 I'm trying to write a function with recur that cut the sequence as soon as it encounters a repetition ( [1 2 3 1 4] should return [1 2 3] ), this is my function: (defn cut-at-repetition [a-seq] (loop[[head & tail] a-seq, coll '()] (if (empty? head) coll (if (contains? coll head) coll (recur (rest tail) (conj coll head)))))) The first problem is with the contains? that throws an exception, I tried replacing it with some but with no success. The second problem is in the recur part which will

clojure - contains?, conj and recur

北慕城南 提交于 2019-12-25 15:20:02
问题 I'm trying to write a function with recur that cut the sequence as soon as it encounters a repetition ( [1 2 3 1 4] should return [1 2 3] ), this is my function: (defn cut-at-repetition [a-seq] (loop[[head & tail] a-seq, coll '()] (if (empty? head) coll (if (contains? coll head) coll (recur (rest tail) (conj coll head)))))) The first problem is with the contains? that throws an exception, I tried replacing it with some but with no success. The second problem is in the recur part which will

postorder using tail recursion

若如初见. 提交于 2019-12-25 09:24:17
问题 i find this link, http://www.experts-exchange.com/Programming/Algorithms/Q_25205171.html, which suggests a way to do postorder tail recursion. however, it uses 2 stacks, is there a way to do this with only one stack. thanks! below is the java code pasted from the link above: public static final <T> void postorder(Tree<T> root) { Stack<Tree<T>> stack = new Stack<Tree<T>>(); Stack<Tree<T>> traversal = new Stack<Tree<T>>(); stack.push(root); while (!stack.isEmpty()) { Tree<T> node = stack.pop();

Why is this scala concatenation function not tail recursive?

做~自己de王妃 提交于 2019-12-25 08:00:09
问题 I'm trying to write a function repeat(s: String, n: Int) that will concatenate string s n times and return it, but for some reason I am not getting the correct results and am getting an error that it is not tail recursive, and I'm having trouble grasping logically why this wouldn't be tail recursive. Does the recursion have to process before the concatenation can be completed? How would I solve the problem? Making the recursion repeat(s+s, n-1) wouldn't work because it would recurse s too