tail-recursion

Designing tail recursion using java 8

青春壹個敷衍的年華 提交于 2020-01-01 10:51:44
问题 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>

How to create a recursive data structure value in (functional) F#?

a 夏天 提交于 2020-01-01 09:10:12
问题 How can a value of type: type Tree = | Node of int * Tree list have a value that references itself generated in a functional way? The resulting value should be equal to x in the following Python code, for a suitable definition of Tree: x = Tree() x.tlist = [x] Edit : Obviously more explanation is necessary. I am trying to learn F# and functional programming, so I chose to implement the cover tree which I have programmed before in other languages. The relevant thing here is that the points of

How can I express a factorial n! with an F# function, recursive or otherwise?

风格不统一 提交于 2020-01-01 06:45:11
问题 A factorial of a natural number (any number greater or equal than 0 ) is that number multiplied by the factorial of itself minus one, where the factorial of 0 is defined as 1 . For example: 0! = 1 1! = 1 * 0! 2! = 2 * 1! 3! = 3 * 2! 4! = 4 * 3! 5! = 5 * 4! Another way of writing this is to multiply all natural numbers between 1 and n for n! : 5! = 1 * 2 * 3 * 4 * 5 How can I express this with a recursive function in F#? And should I do it with a recursive function? //Factorials! let factorial

Tail recursion with Groovy

╄→尐↘猪︶ㄣ 提交于 2020-01-01 05:20:08
问题 I coded 3 factorial algorithms: First, I expect to fail by Stack Overflow. No problem. Second, I try tail recusive call , convert previous algorithm from recursive to iterative. It doesn't work but I don't understand why . Third, I use trampoline() method and works fine as I expect. def factorial factorial = { BigInteger n -> if (n == 1) return 1 n * factorial(n - 1) } factorial(1000) // Stack Overflow factorial = { Integer n, BigInteger acc = 1 -> if (n == 1) return acc factorial(n - 1, n *

What does “sibling calls” mean?

十年热恋 提交于 2020-01-01 04:02:10
问题 On GCC manual, -foptimize-sibling-calls Optimize sibling and tail recursive calls. I know tail recursive calls, for example int sum(int n) { return n == 1 ? 1 : n + sum(n-1); } However, what does sibling calls mean? 回答1: the compiler considers two functions as being siblings if they share the same structural equivalence of return types, as well as matching space requirements of their arguments. http://www.drdobbs.com/tackling-c-tail-calls/184401756 回答2: It must be something like this: int

Prolog recursively count numbers in a list

那年仲夏 提交于 2019-12-31 03:51:38
问题 I need a program to count all the numbers in a list, no matter how DEEPLY NESTED they are. I was able to count numbers in the case where they were not inside another list, but recursing through deeply nested elements is not working out. I have this so far: count([],0). count([H|Tail], N) :- count(Tail, N1), ( number(H) ->N is N1 + 1 ; is_list(H) -> count(H,N) ; N = N1 ). So, if I were to call count([a,1,[2,b],3],N) , the output should be N=3 ; however, I only get N=2 . Could someone please

Is my rec function tail recursive?

橙三吉。 提交于 2019-12-31 02:17:07
问题 Is this function tail-recursive ? let rec rec_algo1 step J = if step = dSs then J else let a = Array.init (Array2D.length1 M) (fun i -> minby1J i M J) let argmin = a|> Array.minBy snd |> fst rec_algo1 (step+1) (argmin::J) In general, is there a way to formally check it ? Thanks. 回答1: This function is tail-recursive; I can tell by eyeballing it. In general it is not always easy to tell. Perhaps the most reliable/pragmatic thing is just to check it on a large input (and make sure you are

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

Generating Fibonacci series in F#

一曲冷凌霜 提交于 2019-12-30 02:46:07
问题 I'm just starting to learn F# using VS2010 and below is my first attempt at generating the Fibonacci series. What I'm trying to do is to build a list of all numbers less than 400. let fabList = let l = [1;2;] let mutable a = 1 let mutable b = 2 while l.Tail < 400 do let c = a + b l.Add(c) let a = b let b = c My first problem is that on the last statement, I'm getting an error message "Incomplete structured construct at or before this point in expression" on the last line. I don't understand

Explanation of lists:fold function

筅森魡賤 提交于 2019-12-29 09:05:28
问题 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