tail-recursion

Does MATLAB perform tail call optimization?

流过昼夜 提交于 2019-12-12 08:22:40
问题 I've recently learned Haskell, and am trying to carry the pure functional style over to my other code when possible. An important aspect of this is treating all variables as immutable, i.e. constants. In order to do so, many computations that would be implemented using loops in an imperative style have to be performed using recursion, which typically incurs a memory penalty due to the allocation a new stack frame for each function call. In the special case of a tail call (where the return

why am I getting “application not a procedure”?

久未见 提交于 2019-12-12 06:58:49
问题 I am trying to write a procedure that computes f by means of an iteratve process. The function f is defined by the rule that f(n) = n, if n < 4 and f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) + 4f(n - 4), if n >= 4. Here is my procedure: (define (f n) (define (newF temp n) (letrec ((first (- n 1)) (second (- n 2)) (third/fourth (- n 3)) (fifth (- n 4))) (define (d) ((if (< first 4) (set! temp (+ temp first)) (newF temp first)) (if (< second 4) (set! temp (+ temp (* second 2))) (newF temp second))

Understanding a tail-recursive vector

醉酒当歌 提交于 2019-12-11 23:24:11
问题 I have a tail-recursive function which converts a vector into a list. I understand each line individually, but have a couple questions: Firstly, in the code what does the code cons ((vector-ref v i) r) (- i 1) mean? (Marked "Q1".) I know that it takes the i 'th element of vector v and concatenates it with i-1 , but why have to be i-1 ? Why not work with i+1 ? e.g. if the vector v length is total 5, then element number 5 is concatenated with the number 4. I understand that it is making the

Tail recursion calling tail recursion

五迷三道 提交于 2019-12-11 12:48:55
问题 I'm trying to solve the pascal's triangle with tail recursion. I understand to do tail recursion, the function call statement should be the last instruction. Like here: (defn pascal [line colum, acc] (if (or (= line 0) (= line colum) (= colum 0)) (+ acc 1) (recur (dec line) colum (pascal (dec line) (dec colum), acc)))) My question is: Since I use a recursive call as a parameter for my recursion, is it still valid? Because I can not replace this: (recur (dec line) colum (pascal (dec line) (dec

Tail Recursive Combinations

☆樱花仙子☆ 提交于 2019-12-11 12:33:27
问题 I have this code: let rec combinations acc listC = match listC with | [] -> acc | h::t -> let next = [h]::List.map (fun t -> h::t) acc @ acc combinations next t It looks tail recursive, but I keep getting stack overflows with it. Any ideas on how to make it work? 回答1: combinations is tail recursive. Your problem is with the @ operator. Appending a list with it iterates the whole list, so as your acc becomes large, you will get a SO. You can see here, that the @ operator is not tail recursive.

How do I mitigate memory leaks when recursively calling a function inside a Future[T]?

天涯浪子 提交于 2019-12-11 07:10:13
问题 This question is inspired by some comments in an earlier Stackoverflow article on the same topic and also motivated by some code I'm writing as well. Given the example contained therein, I'm somewhat convinced that this pattern is tail recursive. If this is the case, how do I mitigate the memory leak posed by accumulating futures whose underlying threads never join the ForkJoinPool from which they were spawned? import com.ning.http.client.AsyncHttpClientConfig.Builder import play.api.libs

RecursionError that happens only without a debugger

。_饼干妹妹 提交于 2019-12-11 07:00:51
问题 I am working on something that needs a recursive calling. The bigger app crashed. While minimizing a problem, I am experiencing a rather strange phenomena. consider the following code: def merge(A, s, m, e): left = A[s:m+1] right = A[m+1:e+1] i = 0 j = 0 k = s while(i < len(left) and j < len(right)): if left[i] < right[j]: A[k] = left[i] k+=1 i+=1 else: A[k] = right[j] k+=1 j+=1 while i < len(left): A[k] = left[i] k+=1 i+=1 while j < len(right): A[k] = right[j] k+=1 j+=1 return A def

Generating a list causes a stack overflow

不想你离开。 提交于 2019-12-11 06:39:15
问题 I'm generating a list of random numbers: let gen n = let rec pom l n = match n with | 0 -> l | _ -> let el = Random.int 20000000 in pom (el::l) (n-1) in pom [] n let lo = gen 1000000 What I get is Fatal error: exception Stack_overflow Why? I'm using tail recursion (and an accumulator) EDIT : You're right, the stack overflows on both sorts. But if my code had a zillion lines, it would be a pain to debug it this way. I'd like to use ocamldebug here, just as a learning experience. I ran

How usable is non tail recursive recursion in Scala?

岁酱吖の 提交于 2019-12-11 02:28:54
问题 Since non tail recursive recursion calls use stack frames like Java does, I'd be weary to do any recursion that, let's say goes beyond 1,000 times. I would be thus weary to use it for most of things. Do people actually use non tail recursive recursion in Scala? If so, what are the criteria I can use to determine if it can be non tail recursive? Also, are there plans to remove this memory restriction in future versions of Scala? 回答1: In the situation where you cannot use single tail recursion,

What reasoning lead to `Sequence expression containing recursive definition is compiled incorrectly`

一曲冷凌霜 提交于 2019-12-10 19:23:26
问题 The question Stack overflow despite tail call position but only in 64-bit lead to uncovering a bug in the F# compiler. After reading the answer I am curious as to the reasoning that lead to finding the bug as I would like to better improve my skills at resolving problems and understanding TCO. 回答1: My reasoning was something like this: Talking about "tail calls" when looking at a computation expression can be misleading - in general there really isn't such a thing (see e.g. this other answer