tailrecursion-modulo-cons

Tail-recursive bounded stream of pairs of integers (Scala)?

时光怂恿深爱的人放手 提交于 2019-12-18 02:37:06
问题 I'm very new to Scala, so forgive my ignorance! I'm trying to iterate of pairs of integers that are bounded by a maximum. For example, if the maximum is 5, then the iteration should return: (0, 0), (0, 1), ..., (0, 5), (1, 0), ..., (5, 5) I've chosen to try and tail-recursively return this as a Stream: @tailrec def _pairs(i: Int, j: Int, maximum: Int): Stream[(Int, Int)] = { if (i == maximum && j == maximum) Stream.empty else if (j == maximum) (i, j) #:: _pairs(i + 1, 0, maximum) else (i, j)

scheme tail recursion

本小妞迷上赌 提交于 2019-12-10 19:10:07
问题 I am trying to create a scheme tail recursive function flatten-tl-rec that flattens a nested list of lists. (define flatten-tl-rec (lambda (xs) (letrec ([flatten-tl-rec-acc (lambda (xs acc) (cond ((empty? xs) acc) ((list? (first xs)) (flatten-tl-rec-acc (rest xs) (append (flatten-tl-rec-acc (first xs) '()) acc))) (else (flatten-tl-rec-acc (rest xs) (cons (first xs) acc)))) )]) (flatten-tl-rec-acc xs '())))) (flatten-tl-rec '(1 2 3 (4 5 6) ((7 8 9) 10 (11 (12 13))))) But I am getting (13 12 11

Tail-recursive bounded stream of pairs of integers (Scala)?

﹥>﹥吖頭↗ 提交于 2019-11-28 23:27:03
I'm very new to Scala, so forgive my ignorance! I'm trying to iterate of pairs of integers that are bounded by a maximum. For example, if the maximum is 5, then the iteration should return: (0, 0), (0, 1), ..., (0, 5), (1, 0), ..., (5, 5) I've chosen to try and tail-recursively return this as a Stream: @tailrec def _pairs(i: Int, j: Int, maximum: Int): Stream[(Int, Int)] = { if (i == maximum && j == maximum) Stream.empty else if (j == maximum) (i, j) #:: _pairs(i + 1, 0, maximum) else (i, j) #:: _pairs(i, j + 1, maximum) } Without the tailrec annotation the code works: scala> _pairs(0, 0, 5)

a tail-recursion version list appending function

别说谁变了你拦得住时间么 提交于 2019-11-27 16:05:54
i see several examples of implementing append an element to a list, but all are not using tail recursion . how to implement such a function in a functional style? (define (append-list lst elem) expr) The following is an implementation of tail recursion modulo cons optimization, resulting in a fully tail recursive code. It copies the input structure and then appends the new element to it, by mutation, in the top-down manner. Since this mutation is done to its internal freshly-created data, it is still functional on the outside (does not alter any data passed into it and has no observable

Should I avoid tail recursion in Prolog and in general?

寵の児 提交于 2019-11-27 09:40:46
I'm working through "Learn Prolog now" online book for fun. I'm trying to write a predicate that goes through each member of a list and adds one to it, using accumulators. I have already done it easily without tail recursion. addone([],[]). addone([X|Xs],[Y|Ys]) :- Y is X+1, addone(Xs,Ys). But I have read that it is better to avoid this type of recursion for performance reasons. Is this true? Is it considered 'good practice' to use tail recursion always? Will it be worth the effort to use accumulators to get into a good habit? I have tried to change this example into using accumulators, but it

a tail-recursion version list appending function

不羁的心 提交于 2019-11-26 17:25:19
问题 i see several examples of implementing append an element to a list, but all are not using tail recursion . how to implement such a function in a functional style? (define (append-list lst elem) expr) 回答1: The following is an implementation of tail recursion modulo cons optimization, resulting in a fully tail recursive code. It copies the input structure and then appends the new element to it, by mutation, in the top-down manner. Since this mutation is done to its internal freshly-created data

Should I avoid tail recursion in Prolog and in general?

回眸只為那壹抹淺笑 提交于 2019-11-26 14:48:37
问题 I'm working through "Learn Prolog now" online book for fun. I'm trying to write a predicate that goes through each member of a list and adds one to it, using accumulators. I have already done it easily without tail recursion. addone([],[]). addone([X|Xs],[Y|Ys]) :- Y is X+1, addone(Xs,Ys). But I have read that it is better to avoid this type of recursion for performance reasons. Is this true? Is it considered 'good practice' to use tail recursion always? Will it be worth the effort to use

Explanation of a Prolog algorithm to append two lists together

这一生的挚爱 提交于 2019-11-26 11:39:00
问题 This is an algorithm to append together two lists: Domains list= integer* Predicates nondeterm append(list, list, list) Clauses append([], List, List) :- !. append([H|L1], List2, [H|L3]) :- append(L1, List2, L3). Goal append([9,2,3,4], [-10,-5,6,7,8], Ot). The result is a list [9,2,3,4,-10,-5,6,7,8] , and it\'s saved in \" Ot \". My question is, how does this work? What I understand is that in every recursive call, in the first list, you get only the tail as a list ( thus reducing its size by